2017-08-13 104 views
0

我需要發出POST請求才能申領優惠券。在API文檔,我覺得這一點:Laravel 5 - 使用form_param製作https請求請求

POST /vouchers/{voucherId}/claim 

{ 
    "Participant": { 
     "FirstName": "John", 
     "LastName": "Jones", 
     "Telephone": "99999 127 127", 
     "EmailAddress": "[email protected]", 
     "PostCode": "HP18 9HX", 
     "HouseNameNumber": "2", 
     "Street": "Bridge Road", 
     "Locality": "Ickford", 
     "Town": "Lodnon", 
     "County": "Bucks" 
    }, 
    "ExperienceDate": "2015-10-01T00:00:00" 
} 

現在我使用Laravel狂飲庫我提出這個要求:

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」:[「發生了 錯誤。」 ](截短...)

以下數據向API發送請求的正確方法是什麼?

回答

0

試試這個

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

如果您的API只接受JSON,嘗試用json

+0

感謝取代form_params。那是對的 –