COil
COil

Reputation: 7606

Bad request returned when using the DELETE curl method with Guzzle and sendgrid

I'm trying to use the sendgrid v3 API to purge bounces, it works fine when using CURL in CLI, here is the command:

curl -v -X DELETE -d '{"delete_all": true}' -H "Content-Type: application/json" -H "Authorization: Bearer SG.mykey" "https://api.sendgrid.com/v3/suppression/bounces"

But when trying to launch it with Symfony2 / Guzzle, I am getting a bad request error, however the request seems OK, here is the output of (string) $request:

"""
DELETE /v3/suppression/bounces HTTP/1.1\r\n
Host: api.sendgrid.com\r\n
Authorization: Bearer SG.mykey\r\n
User-Agent: Guzzle/3.9.3 curl/7.35.0 PHP/5.5.9-1ubuntu4.17\r\n
Accept: application/json\r\n
Content-Length: 20\r\n
\r\n
{"delete_all": true}
"""

And the exception:

[Guzzle\Http\Exception\ClientErrorResponseException]   
Client error response                                  
[status code] 400                                      
[reason phrase] BAD REQUEST                            
[url] https://api.sendgrid.com/v3/suppression/bounces

When using the GET method, it works correctly and it returns me all the bounces.

Here is the guzzle code:

$request = $this->httpClient->delete('/v3/suppression/bounces', null, '{"delete_all": true}');
$response = $request->send();

The http client is a service initialized with the https://api.sendgrid.com base URL.

Upvotes: 0

Views: 995

Answers (2)

COil
COil

Reputation: 7606

Answering to myself. The problem was pretty obvious: The content-type header was not set, the "accept" one was. I didn't care about this header because you don't have to pass it when using the GET method for this API. So now when debugging my request object I have:

"""
DELETE /v3/suppression/bounces HTTP/1.1\r\n
Host: api.sendgrid.com\r\n
Authorization: Bearer SG.mykey\r\n
Content-Type: application/json\r\n
Content-Length: 20\r\n
User-Agent: Guzzle/3.9.3 curl/7.35.0 PHP/5.5.9-1ubuntu4.17\r\n
Accept: application/json\r\n
\r\n
{"delete_all": true}
"""

Upvotes: 0

Gavin Love
Gavin Love

Reputation: 595

Your problem I think is that you are sending 3 params to delete when it only has two, instead what you need to do is pass the body in options array.

$response = $this->httpClient->delete(
        '/v3/suppression/bounces',
        [
            'body' => json_encode(['delete_all', true]),
            'Authorization' => 'Basic ' . base64_encode($username . ':' . $password),
            'content-type'  => 'application/json'
        ]
    );

Guzzle options docs

Upvotes: 1

Related Questions