【问题标题】:Laravel 5 - make https guzzle request with form_paramLaravel 5 - 使用 form_param 发出 https guzzle 请求
【发布时间】: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


    【解决方案1】:

    试试这个

    ...
    'form_params' => [
        'Participant' => [
            'FirstName' => 'Peter',
            'LastName' => 'Alexo',
            'Telephone' => '8888888888888'
        ],
        'ExperienceDate' => '2015-10-01T00:00:00'
    ]
    ...
    

    如果您的 api 只接受 json,请尝试将 form_params 替换为 json

    【讨论】:

      猜你喜欢
      • 2017-11-15
      • 2015-06-03
      • 2020-07-14
      • 1970-01-01
      • 2019-01-26
      • 2018-08-28
      • 2018-03-21
      • 2017-12-25
      • 2018-09-15
      相关资源
      最近更新 更多