简体   繁体   中英

guzzlephp in Laravel : send post request with form-data parameters like postman form-data

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'
        ]
    ]
]);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM