Guzzle POST 总是请求 returns 400 错误请求

Guzzle POST request always returns 400 Bad Request

我一直在尝试使用 Guzzle 向具有负载的端点发出简单的 POST 请求,但我总是返回 400 错误请求。

我可以在 Postman 中发出相同的请求并且它有效。此外,如果我使用 cURL 发出请求,它会起作用。

谁能从我的代码中看出我做错了什么?

这是原始的 cURL 请求:

curl "https://endpoint.com/" \
  -H "Authorization: ApiKey pp_test_*********" \
  --data '{
    "shipping_address": {
      "recipient_name": "Deon Botha",
      "address_line_1": "Eastcastle House",
    "address_line_2": "27-28 Eastcastle Street",
    "city": "London",
    "county_state": "Greater London",
    "postcode": "W1W 8DH",
    "country_code": "GBR"
    },
    "customer_email": "email£example.com",
    "customer_phone": "123455677",
    "customer_payment": {
      "amount": 29.99,
      "currency": "USD"
    },
    "jobs": [{
      "assets": ["http://psps.s3.amazonaws.com/sdk_static/1.jpg"],
    "template_id": "i6_case"
    }, {
      "assets": ["http://psps.s3.amazonaws.com/sdk_static/2.jpg"],
    "template_id": "a1_poster"
    }]
  }' 

还有 Postman PHPHttp 请求也可以。

<?php

$request = new HttpRequest();
$request->setUrl('https://endpoint.com/');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'Connection' => 'keep-alive',
  'Content-Length' => '678',
  'Accept-Encoding' => 'gzip, deflate',
  'Host' => 'api.kite.ly',
  'Cache-Control' => 'no-cache',
  'Accept' => '*/*',
  'User-Agent' => 'PostmanRuntime/7.19.0',
  'Content-Type' => 'text/plain',
  'Authorization' => 'ApiKey pk_test_*******'
));

$request->setBody(' {
    "shipping_address": {
      "recipient_name": "Deon Botha",
      "address_line_1": "Eastcastle House",
    "address_line_2": "27-28 Eastcastle Street",
    "city": "London",
    "county_state": "Greater London",
    "postcode": "W1W 8DH",
    "country_code": "GBR"
    },
    "customer_email": "email@example.com",
    "customer_phone": "12345667",
    "customer_payment": {
      "amount": 29.99,
      "currency": "USD"
    },
    "jobs": [{
      "assets": ["http://psps.s3.amazonaws.com/sdk_static/1.jpg"],
    "template_id": "i6_case"
    }, {
      "assets": ["http://psps.s3.amazonaws.com/sdk_static/2.jpg"],
    "template_id": "a1_poster"
    }]
  }');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
} 

但是当我尝试使用 Guzzle 发送相同的请求时,它失败并显示 400 Bad Request,我不明白为什么。


        $data = [
            "shipping_address" => [
                "recipient_name" => "Deon Botha",
                "address_line_1" => "Eastcastle House",
                "address_line_2" => "27-28 Eastcastle Street",
                "city" => "London",
                "county_state" => "Greater London",
                "postcode" => "W1W 8DH",
                "country_code" => "GBR"
            ],
            "customer_email" => "example@email.com",
            "customer_phone" => "+44 (0)784297 1234",
            "customer_payment" => [
                "amount" => 29.99,
                "currency" => "USD"
            ],
            "jobs" =>
                [
                    "assets" => ["http://psps.s3.amazonaws.com/sdk_static/1.jpg"],
                    "template_id" => "i6_case"
                ]

        ];

       $options = json_encode($data);

        $response = $client->request('POST', config('services.endpoint.com'),
            ['headers' => ["Authorization" => config('services.endpoint.com.public_key'),
              'Content-Type' => "application/json"], $options]);

如果有人能帮我调试一下,我将不胜感激。

我正在使用 Unirest 发出任何类型的 HTTP 请求。我尝试使用 Guzzle,但遇到了与您相同的问题。

所以我所做的就是在我的项目中安装 Unirest,所有过程在他们的文档中都有详细说明。这对我来说非常好。

如果您使用的是 Guzzle 6(您可能应该使用),那么您实际构建的方式比您需要的更复杂,因此端点未收到预期的 JSON。试试这个:

$client = new Client([
     'base_uri' => 'https://my.endpoint.com/api',
     'headers' => [
          'Accept' => 'application/json',
          ...other headers...
     ]
]);

$data = [...your big slab of data...];

$response = $client->post('/kitely/path', ['json' => $data]);

// a string containing the results, which will depend on the endpoint
// the Accept header says we will accept json if it is available
// then we can use json_decode on the result
$result = $response->getBody()->getContents();