与Guzzle和sendgrid一起使用DELETE curl方法时返回错误请求

时间:2016-06-17 08:49:58

标签: symfony curl sendgrid guzzle

我正在尝试使用sendgrid v3 API to purge bounces,它在CLI中使用CURL时工作正常,这是命令:

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"

但是当尝试使用Symfony2 / Guzzle启动它时,我收到一个错误的请求错误,但请求似乎没问题,这里是(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}
"""

例外:

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

使用GET方法时,它可以正常工作,并返回所有反弹。

以下是guzzle代码:

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

http客户端是使用https://api.sendgrid.com基本URL初始化的服务。

2 个答案:

答案 0 :(得分:1)

你的问题我认为你发送的3个参数只有两个,而你需要做的就是传递选项数组中的主体。

$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

答案 1 :(得分:0)

回答自己。问题非常明显:没有设置内容类型标题,"接受"一个是。我并不关心这个标题,因为在使用此API的GET方法时,您不必通过它。所以现在在调试我的请求对象时,我有:

"""
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}
"""