DELETE request with parameters using Guzzle

Xavier Ojeda Aguilar picture Xavier Ojeda Aguilar · Sep 28, 2015 · Viewed 10k times · Source

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.

Answer

Shaun Bramley picture Shaun Bramley · Sep 29, 2015

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