0
votes

Solved This is for use with the ecomdash api as it turns out when using this method all json strings must be enclosed in square brackets as if there was an array of products. So this actually works as expected with json_encode for an array of products but with only one product being updated you have to manually add the square brackets around the json. So because of this you have to use body not json when setting up the request. so here is the final code that worked.

$data[] = array("Sku" => "067567", "Quantity" => 21, "WarehouseId" => 28345);
    $dataJson = json_encode($data);

    //put square brackets in if only one product is being updated
    if(count($data) == 1) {
      $dataJson = '['.$dataJson.']';
    }

    $headers = $this->auth;
    $headers['Content-Type'] = 'application/json';
    $params = array('headers' => $headers,
                    'json' => $data);

    $response = $this->client->request('POST', 'inventory/updateQuantityOnHand', array('headers' => $headers, 'body' => $dataJson));

    var_dump(json_decode($response->getBody(), true));
    die();
  }

I have searched everywhere and tried a million things but I keep getting the same error when trying to send json as a POST request with guzzle.

The headers I am sending are just credentials for the api I am using and they are working with other requests so I don;'t think they are the issue.

here is some of the code I have used.

$data = array("Sku" => "067567", "Quantity" => 10, "WarehouseId" => 
  28345);
$dataJson = json_encode($data, JSON_FORCE_OBJECT);
$headers = $this->auth;
$headers['Content-Type'] = 'application/json';
$params = array('headers' => $headers,
                'body' => $dataJson);

$response = $this->client-
   >post('inventory/updateQuantityOnHand',$params);



$data = array("Sku" => "067567", "Quantity" => 10, "WarehouseId" => 
  28345);
$headers = $this->auth;
$headers['Content-Type'] = 'application/json';
$params = array('headers' => $headers,
                'json' => $data);

$response = $this->client-
  >post('inventory/updateQuantityOnHand',$params);

the error I am getting is

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://ecomdash.azure-api.net/api/inventory/updateQuantityOnHand resulted in a 400 Bad Request response: {"Message":"The body of your request was missing or invalid","ExceptionMessage":"The body of your request was missing or invalid","ExceptionType":"System.InvalidOperationException","StackTrace":null} (truncated...) in /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111 Stack trace: #0 /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response)) #2 /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promis in /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 111

info from the debug flag

> POST /api/inventory/updateQuantityOnHand HTTP/1.1
Host: ecomdash.azure-api.net
User-Agent: GuzzleHttp/6.2.1 curl/7.52.1 PHP/7.1.8
Content-Type: application/json
ecd-subscription-key: *****************
Ocp-Apim-Subscription-Key: ****************
Content-Length: 50

* upload completely sent off: 50 out of 50 bytes
< HTTP/1.1 400 Bad Request
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Length: 199
< Content-Type: application/json; charset=utf-8
< Expires: -1
< Request-Context: appId=cid-v1:6baf8227-a96f-4757-ac3b-a170d33a7b16
< Set-Cookie: ARRAffinity=5b826c9996f848edeab28288985b46ca9013ce4aef93b8f195bc66f2f91c578c;Path=/;HttpOnly;Domain=ecomdashapi.azurewebsites.net
< X-AspNet-Version: 4.0.30319
< X-Powered-By: ASP.NET
< Date: Wed, 07 Feb 2018 17:02:00 GMT
< 
* Curl_http_done: called premature == 0
* Connection #0 to host ecomdash.azure-api.net left intact

I am using Guzzle version 6.2.1

Thanks in advance for any help this is killing me.

1
Are you trying "both" code at the same time?Felippe Duarte
No just put them there as two examples of things I have tried. Both resulted in the same error though.Donniep
You can pass a , ['debug' => true] parameter for debugging purposesFelippe Duarte
I added the returned info from the debug to the questionDonniep
That's strange. Can you show us the structure of $this->auth? But remeber to replace any password or sensitive information with ***Felippe Duarte

1 Answers

1
votes

This is for use with the ecomdash API as it turns out when using this method all JSON strings must be enclosed in square brackets as if there was an array of products. So this actually works as expected with json_encode for an array of products but with only one product being updated you have to manually add the square brackets around the JSON. So because of this you have to use body not JSON when setting up the request. So here is the final code that worked.

$data[] = array("Sku" => "067567", "Quantity" => 21, "WarehouseId" => 28345);
$dataJson = json_encode($data);

//put square brackets in if only one product is being updated
if(count($data) == 1) {
  $dataJson = '['.$dataJson.']';
}

$headers = $this->auth;
$headers['Content-Type'] = 'application/json';

$response = $this->client->request('POST', 'inventory/updateQuantityOnHand', array('headers' => $headers, 'body' => $dataJson));

var_dump(json_decode($response->getBody(), true));
    // die()
    die();
}