耗时POST请求始终返回400错误请求

用户名

我一直在尝试使用Guzzle向带有有效负载的端点发出一个简单的POST请求,但是我总是收到400 Bad Request返回。

我可以在邮递员中提出相同的请求,并且它可以工作。另外,如果我使用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"
    }]
  }' 

邮递员PHPHttp Request也可以。

<?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 protected]",
    "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" => "[email protected]",
            "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]);

如果有人可以帮助我进行调试,我将非常感激。

杰森

如果您正在使用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();  

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

httr对API的POST请求返回400错误

来自分类Dev

尝试获取POST以返回400错误请求

来自分类Dev

WCF Post方法,返回400错误请求

来自分类Dev

HttpURLConnection POST返回400错误请求

来自分类Dev

Localhost WEB Api始终返回400错误请求

来自分类Dev

Python POST 请求 400 错误请求

来自分类Dev

HttpListener返回错误请求400

来自分类Dev

POST 400错误,ajax请求

来自分类Dev

axios.post返回400 React Native的错误请求

来自分类Dev

VB.NET-Salesforce POST请求返回400错误的请求错误

来自分类Dev

Python请求POST-400-Python请求的错误请求

来自分类Dev

为什么此Python POST请求返回400错误的请求代码?

来自分类Dev

AJAX POST中的400错误的请求错误

来自分类Dev

意外错误400:错误请求HttpBuilder POST请求

来自分类Dev

Google安全浏览返回400错误请求

来自分类Dev

使用DotNetOpenAuth返回Amazon(400)错误请求

来自分类Dev

使用DotNetOpenAuth返回Amazon(400)错误请求

来自分类Dev

Upnp:GetProtocolInfo返回400(错误请求)

来自分类Dev

AJAX POST请求中的HTTP错误400

来自分类Dev

AngularJS中的POST 400错误请求

来自分类Dev

POST方法是ajax给出400(错误请求)

来自分类Dev

$http post 方法的 400 错误请求

来自分类Dev

C++ Http POST 400 错误请求

来自分类Dev

为什么HTTP POST返回代码400(错误请求)?HTTP POST方法

来自分类Dev

Facebook Graphi API用户请求返回400错误请求

来自分类Dev

IdentityServer3 ResourceOwner Angular请求返回400错误请求

来自分类Dev

带有python 400错误请求的POST请求

来自分类Dev

耗时POST请求不起作用

来自分类Dev

错误的请求错误 400