Reputation: 57
I want to send data with post Request to an API. With postman this API only worked when i set parameters as a form-data in body like this picture
This is my code
$params = array(
'From ' => 'THR',
'To' => 'KIH',
'Date' => '2019/09/17' ,
'AdultCount' => '1' ,
'ChildCount' => '0',
'InfantCount' => '0',
'ApiSiteID' => '198-10001-1-10' ,
'UserID' => '3df91-s77' ,
);
$client = new Client(['base_uri' => 'http://something.site.ir/api/FlightReservationApi/'] );
$response = $client->request('POST', 'SearchFlightData' ,
[ 'form_params' => [ $params] ] );
$body= $response->getBody();
$remainingBytes = $body->getContents();
dd($remainingBytes);
But it's not worked for me. i use this all of parameters (json , form_params , body ) and none of them dosen't work for me
I use laravel 5.4 , and guzzle 6.3
According to guzzlephp document this config for application/x-www-form-urlencoded sending parameters but i need a config for form-data sending parameters
Sending application/x-www-form-urlencoded POST requests requires that you >specify the POST fields as an array in the form_params request options.
$response = $client->request('POST', 'http://httpbin.org/post', [
'form_params' => [
'field_name' => 'abc',
'other_field' => '123',
'nested_field' => [
'nested' => 'hello'
]
]
]);
Upvotes: 1
Views: 4626
Reputation: 48
It should be
$client = new Client(['base_uri' => 'http://something.site.ir/api/FlightReservationApi/'] );
$response = $client->request('POST', 'SearchFlightData' ,
[ 'form_params' => $params ] );
instead of
$client = new Client(['base_uri' => 'http://something.site.ir/api/FlightReservationApi/'] );
$response = $client->request('POST', 'SearchFlightData' ,
[ 'form_params' => [ $params] ] );
The fact that you're wrapping an array ($params) in another array might be the issue.
Upvotes: 3