我一直在尝试使用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"
}]
}'
邮递员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]);
如果有人可以帮助我进行调试,我将非常感激。
答案 0 :(得分:1)
如果您正在使用Guzzle 6(也许应该使用),则实际上是在以比所需的方式更复杂的方式进行构造,以使终结点无法接收到预期的JSON。尝试以下方法:
UPDATE outputtable,
(SELECT Sum(total) SUM,
Year(date) year,
Month(date) month,
Day(date) day,
Hour(date) hour
FROM inputtable
GROUP BY Year(date),
Month(date),
Day(date),
Hour(date)) InputT SET total = InputT.sum WHERE Year(outputtable.date) = InputT.year
AND Month(outputtable.date) = InputT.month
AND Day(outputtable.date) = InputT.day
AND Hour(outputtable.date) = InputT.hour;
答案 1 :(得分:0)
我正在使用Unirest发出各种HTTP请求。我尝试使用Guzzle,但面临的问题与您一样。
所以我所做的就是在项目中安装Unirest,所有过程在其文档中都有详细说明。对我来说这很好。