无法将 json 作为 guzzle 帖子正文发送

问题描述 投票:0回答:2

解决了 这是与 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
响应:{“Message”:“您的请求正文丢失或无效”,“ExceptionMessage”:“请求正文您的请求丢失或无效","ExceptionType":"System.InvalidOperationException","StackTrace":null} (截断...) 在 /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/guzzle/src/异常/RequestException.php:111堆栈跟踪:#0 /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/guzzle/src/Middleware.php(65):GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp) \Psr7\Request), 对象(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/Promise.php(156): /Applications/MAMP/htdocs 中的 GuzzleHttp\Promis /sunglasses/libraries/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php 第 111 行

来自调试标志的信息

> 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

预先感谢您提供的任何帮助,这让我丧命。

php json guzzle guzzle6
2个回答
2
投票

这适用于 ecomdash API,因为事实证明,使用此方法时,所有 JSON 字符串都必须括在方括号中,就像有一个产品数组一样。因此,对于一系列产品,这实际上可以按预期使用 json_encode,但只有一个产品被更新,您必须手动在 JSON 周围添加方括号。因此,在设置请求时,您必须使用正文而不是 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();
}

0
投票
$client = new Client();
$URI = 'complete_url';
$params['headers'] = ['Content-Type' => 'application/json'];
$params = [
    'json' => [
       'kay1' => 'val1',
       'kay2' => 'val2',
     ]
    ];
$response = $client->post($URI, $params);
echo print_r(json_decode($response->getBody(),true));
die();
© www.soinside.com 2019 - 2024. All rights reserved.