无法发送json作为guzzle post body

时间:2018-02-07 16:40:13

标签: php json guzzle guzzle6

解决 这与ecomdash api一起使用,因为当使用此方法时,所有json字符串必须用方括号括起来,就像有一系列产品一样。因此,对于产品数组,这实际上与json_encode一样正常工作,但只有一个产品正在更新,您必须手动添加json周围的方括号。因此,在设置请求时,您必须使用body而不是json。所以这是最终的代码。

$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();
  }

我已经到处搜索并尝试了一百万件事情,但在尝试使用guzzle发送json作为POST请求时,我仍然遇到同样的错误。

我发送的标题只是我正在使用的api的凭据,他们正在处理其他请求,所以我不知道;并且认为它们是问题。

这是我使用过的一些代码。

$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);

我得到的错误是

  

致命错误:未捕获GuzzleHttp \ Exception \ ClientException:客户端错误:POST https://ecomdash.azure-api.net/api/inventory/updateQuantityOnHand导致400 Bad Request响应:{"消息":"您的请求正文缺失或无效"," ExceptionMessage":"您的请求正文丢失或无效"," ExceptionType":" System.InvalidOperationException& #34;," StackTrace":null}(截断...)/Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111堆栈跟踪:#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}(对象(GuzzleHttp \ Psr7 \)回复))#2 /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/promises/src/Promise.php(156):顾第111行/Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php中的zzleHttp \ Promis

来自调试标志的

信息

> 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

我正在使用Guzzle版本6.2.1

提前感谢您的任何帮助,这对我造成了伤害。

1 个答案:

答案 0 :(得分:1)

这与ecomdash API一起使用,因为在使用此方法时,所有JSON字符串都必须用方括号括起来,就好像有一系列产品一样。因此,对于一系列产品,这实际上与json_encode一样正常工作,但只有一个产品正在更新,您必须手动添加JSON周围的方括号。因此,在设置请求时,您必须使用body而不是JSON。所以这是最终的代码。

$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();
}