HTTParty 补丁请求在 curl 有效时无效

HTTParty patch request doesn't work when curl does

在 Rails 4.2 中,我正在使用 HTTParty 为 SendGrid 的 API 版本 3 制作一个 API 包装器。这是它的样子:

class SendGridV3
  include HTTParty
  debug_output $stdout

  base_uri 'https://api.sendgrid.com/v3'
  format :json

  headers 'Authorization' => "Bearer #{ENV['SENDGRID_API_KEY']}"

  def self.enforce_tls
    response = patch '/user/settings/enforced_tls', query: {require_tls: true}.to_json
    puts response
    response['require_tls']
  end
end

当我 运行 SendGridV3.enforce_tls 时,预期响应是 {"require_tls": true, "require_valid_cert": false} 但实际响应是 {"require_tls": false, "require_valid_cert": false}

这是debug_output

的输出
opening connection to api.sendgrid.com:443...
opened
starting SSL for api.sendgrid.com:443...
SSL established
<- "PATCH /v3/user/settings/enforced_tls?{%22require_tls%22:true} HTTP/1.1\r\nAuthorization: Bearer MY-SENDGRID-KEY\r\nConnection: close\r\nHost: api.sendgrid.com\r\nContent-Length: 0\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n"
<- ""
-> "HTTP/1.1 200 OK\r\n"
-> "Server: nginx\r\n"
-> "Date: Fri, 25 Aug 2017 18:47:15 GMT\r\n"
-> "Content-Type: application/json\r\n"
-> "Content-Length: 48\r\n"
-> "Connection: close\r\n"
-> "Access-Control-Allow-Methods: HEAD, PATCH, OPTIONS, GET\r\n"
-> "Access-Control-Max-Age: 21600\r\n"
-> "Access-Control-Expose-Headers: Link\r\n"
-> "Access-Control-Allow-Origin: *\r\n"
-> "Access-Control-Allow-Headers: AUTHORIZATION, Content-Type, On-behalf-of, x-sg-elas-acl\r\n"
-> "Content-Security-Policy: default-src https://api.sendgrid.com; frame-src 'none'; object-src 'none'\r\n"
-> "X-Content-Type-Options: nosniff\r\n"
-> "Strict-Transport-Security: max-age=31536000\r\n"
-> "X-Ratelimit-Remaining: 599\r\n"
-> "X-Ratelimit-Limit: 600\r\n"
-> "X-Ratelimit-Reset: 1503686880\r\n"
-> "Powered-By: Mako\r\n"
-> "\r\n"
reading 48 bytes...
-> "{\"require_tls\":false,\"require_valid_cert\":false}"
read 48 bytes
Conn close
{"require_tls"=>false, "require_valid_cert"=>false}
=> false

我还尝试使用 body: 而不是 query: 来传递 JSON

当我使用这个 curl 时,我得到了预期的响应:

curl -X "PATCH" "https://api.sendgrid.com/v3/user/settings/enforced_tls" -H "Authorization: Bearer MY-SENDGRID-KEY" -H "Content-Type: application/json" -d '{"require_tls":true}'

那么,当 HTTParty 失败时,curl 是如何工作的呢?

我认为查询中的 .to_json 导致了问题,HTTParty 使用 ruby hashquery 参数进行解析以创建查询字符串。 .to_json 使 query 参数成为一个字符串,因此查询按原样传递以构造 URI。从那里删除 .to_json 应该可以解决问题。

明确设置 Content-Type 并使用 :body 将使其工作。

class SendGridV3
  ...
  headers 'Content-Type' => 'application/json'
  def self.enforce_tls
    response = patch '/user/settings/enforced_tls', body: {require_tls: true}.to_json
    puts response
    response['require_tls']
  end
end

归因于 OP 注意到在这个用例中 :query 需要 :body