我正在尝试通过 Ruby 实现 Curl 补丁请求。Curl 请求如下所示:
curl https://mywebsite/test/plan \
-X PATCH \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {myJWT}' \
-d '{
"CheckOrNot": "N",
"CheckList": [
{
"id": "123"
},
{
"id": "234"
}
]
}'
当我运行 Curl 命令时,我可以得到:
400 错误请求。
但是当我尝试使用 Ruby 发出如下请求时:
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse("https://mywebsite/test/plan")
request = Net::HTTP::Patch.new(uri)
request.content_type = "application/json"
request["Authorization"] = "Bearer {myJWT}"
request.body = JSON.dump({
"CheckOrNot" => "N",
"plans" => [
{
"id" => "123"
},
{
"id" => "234"
}
]
})
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
我总会得到一个
502 Bad Gateway: 从上游服务器收到无效响应\n。
谁能解释为什么会这样?我该如何解决?有没有更好的方法在 Ruby 中实现 Curl Patch 要求?