使用Guzzle和SendGrid的Delete Curl方法返回不良请求



我正在尝试使用sendgrid v3 api清除弹跳,在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.1rn
Host: api.sendgrid.comrn
Authorization: Bearer SG.mykeyrn
User-Agent: Guzzle/3.9.3 curl/7.35.0 PHP/5.5.9-1ubuntu4.17rn
Accept: application/jsonrn
Content-Length: 20rn
rn
{"delete_all": true}
"""

和例外:

[GuzzleHttpExceptionClientErrorResponseException]   
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 base url。

初始化的服务

我认为您的问题是您在只有两个时发送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选项文档

回答自己。问题很明显:没有设置内容类型的标题,"接受"是。我不在乎此标头,因为在使用此API的GET方法时,您不必通过它。因此,现在在调试我的请求对象时:

"""
DELETE /v3/suppression/bounces HTTP/1.1rn
Host: api.sendgrid.comrn
Authorization: Bearer SG.mykeyrn
Content-Type: application/jsonrn
Content-Length: 20rn
User-Agent: Guzzle/3.9.3 curl/7.35.0 PHP/5.5.9-1ubuntu4.17rn
Accept: application/jsonrn
rn
{"delete_all": true}
"""

最新更新