Xavier Ojeda Aguilar
Xavier Ojeda Aguilar

Reputation: 268

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.

Upvotes: 4

Views: 15674

Answers (3)

Omsun Kumar
Omsun Kumar

Reputation: 147

    $response = json_decode($this->client->delete($uri,$params)->getStatusCode());        
    echo $response;

This will also give the status of the response as 204 or 404

Upvotes: 0

Zoé R.
Zoé R.

Reputation: 93

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

Upvotes: 2

Shaun Bramley
Shaun Bramley

Reputation: 2047

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

Upvotes: 8

Related Questions