首页 文章

无法发送json作为guzzle post body

提问于
浏览
0

Solved 这与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请求时,我仍然遇到同样的错误 .

我发送的 Headers 只是我正在使用的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“,”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 (对象(GuzzleHttp \ Psr7 \ Response))#2 / Applications / MAMP / htdocs / sunglasses / libraries / vendor / guzzlehttp / promises / src / Pr omise.php(156):第111行/Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php中的GuzzleHttp \ Promis

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

我使用的是Guzzle版本6.2.1

在此先感谢任何帮助,这对我造成了伤害 .

1 回答

  • 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();
    

    }

相关问题