【问题标题】:Bad request returned when using the DELETE curl method with Guzzle and sendgrid使用带有 Guzzle 和 sendgrid 的 DELETE curl 方法时返回错误请求
【发布时间】:2016-06-17 08:49:57
【问题描述】:

我正在尝试使用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 初始化的服务。

【问题讨论】:

    标签: symfony curl sendgrid guzzle


    【解决方案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

    【讨论】:

    • 我接受您指定内容类型标头的答案。但这是为 Guzzle6 准备的,感谢您的帮助。
    • 抱歉,我没有注意到您在使用 Guzzle/3.9.3,我只是查看了我们为使其正常工作所做的工作。
    【解决方案2】:

    回答我自己。问题很明显:没有设置内容类型标头,而是设置了“接受”标头。我不关心这个标头,因为在使用此 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}
    """
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      相关资源
      最近更新 更多