Guzzle POST请求始终返回400 Bad Request

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

我一直在尝试使用Guzzle向具有有效负载的端点发出一个简单的POST请求,但始终会返回400 Bad Request。

我可以在Postman中提出相同的请求,并且它可以工作。另外,如果我使用cURL发出请求,它也会起作用。

任何人都可以从我的代码中看出我做错了吗?

这是原始的cURL请求:

curl "https://api.kite.ly/v4.0/print/" \
  -H "Authorization: ApiKey pk_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 Request。

<?php

$request = new HttpRequest();
$request->setUrl('https://api.kite.ly/v4.0/print/');
$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.kitely.url'),
            ['headers' => ["Authorization" => config('services.kitely.public_key'),
              'Content-Type' => "application/json"], $options]);

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

laravel rest guzzle guzzlehttp
1个回答
0
投票

我正在使用Unirest发出各种HTTP请求。我尝试使用Guzzle,但与您一样面临相同的问题。

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

© www.soinside.com 2019 - 2024. All rights reserved.