【发布时间】:2018-01-21 13:55:13
【问题描述】:
我需要发出 POST 请求来领取代金券。在 API 文档中我发现:
POST /vouchers/{voucherId}/claim
{
"Participant": {
"FirstName": "John",
"LastName": "Jones",
"Telephone": "99999 127 127",
"EmailAddress": "hahahah@hahaha.net",
"PostCode": "HP18 9HX",
"HouseNameNumber": "2",
"Street": "Bridge Road",
"Locality": "Ickford",
"Town": "Lodnon",
"County": "Bucks"
},
"ExperienceDate": "2015-10-01T00:00:00"
}
现在我使用 Laravel guzzle 库提出这个请求:
public function testclaim()
{
$client = new GuzzleHttp\Client;
$headers = ['Content-Type' => 'application/json'];
$res = $client->post('https://apidev.asdasd.com/vouchers/11989898_1-9FDD/claim', [
'headers'=>$headers,
'auth' => [
'PET_RES', 'asdasdasd111111'
],
'form_params' => [
'FirstName' => 'Peter',
'LastName' => 'Alexo',
'Telephone' => '8888888888888'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
dd($res);
}
但我得到的是:
400 错误请求
{ "Message": "请求无效。", "ModelState": { "claim": [ "An 发生错误。”](已截断...)
使用以下数据向 API 发送请求的正确方法是什么?
【问题讨论】:
标签: php laravel curl request guzzle