简体   繁体   中英

send raw empty json with GuzzleHttp in laravel

My solution my be simple but honestly spent almost 5 hour and still not found my answer. I need Send request with Post and retrieve my data. With POSTMAN it's work just fine.

在此处输入图片说明

and here my Code

use GuzzleHttp\Client;

 $client =new Client();

     $client->post($url,[
            'form_params' => [
              '{}'
            ]
        ]);

with POSTMAN , I don't need to set Header and all i need to send {} .

Get this Error :

Client error: `POST http://URL` resulted in a `400 
Bad Request` response: {"error":"Failed when parsing body as json"}

Well,How Can I do it now?

It looks like you want to send your POST data without application/x-www-form-urlencoded which is what automatically added by using 'form_params'

If you wish to do this, you should do something more akin to the following:

use GuzzleHttp\Client;
$client = new Client();
$response = $client->post($url, [
    'body' => '{}'
]);

I think you can just do

$client->post($url,[
    'parameter_one' => $value1,
    'param_two' => $value2,
]);

Not sure about how to pass object, i think you can pass PHP object with data as second parameter or array of objects, if you wish..

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