Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DELETE request with parameters using Guzzle

I have to do a DELETE request, with parameters, in the CodeIgnitor platform. First, I tried using cURL, but I switched to Guzzle.

An example of the request in the console is:

curl -X DELETE -d '{"username":"test"}' http://example.net/resource/id

But in the documentation of Guzzle they use parameters just like GET, like DELETE http://example.net/resource/id?username=test, and I don't want to do that.

I tried with:

$client = new GuzzleHttp\Client();
$client->request('DELETE', $url, $data);

but the request just calls DELETE http://example.com/resource/id without any parameters.

like image 386
Xavier Ojeda Aguilar Avatar asked Oct 24 '25 14:10

Xavier Ojeda Aguilar


2 Answers

If I interpret your curl request properly, you are attempting to send json data as the body of your delete request.

// turn on debugging mode.  This will force guzzle to dump the request and response.
$client = new GuzzleHttp\Client(['debug' => true,]);

// this option will also set the 'Content-Type' header.
$response = $client->delete($uri, [
    'json' => $data,
]);
like image 116
Shaun Bramley Avatar answered Oct 27 '25 03:10

Shaun Bramley


coming late on this question after having same. Prefered solution, avoiding debug mode is to pass params in 'query' as :

$response = $client->request('DELETE', $uri, ['query' => $datas]);

$datas is an array Guzzle V6

like image 23
Zoé R. Avatar answered Oct 27 '25 02:10

Zoé R.