4

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

8

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,
]);
Shaun Bramley
  • 1,989
  • 11
  • 16
2

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

Zoé R.
  • 93
  • 1
  • 9
0
    $response = json_decode($this->client->delete($uri,$params)->getStatusCode());        
    echo $response;

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

Omsun Kumar
  • 147
  • 1
  • 3
  • 9