I am trying to implement a Curl patch request by Ruby. The Curl request is something like below:
curl https://mywebsite/test/plan \
-X PATCH \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {myJWT}' \
-d '{
"CheckOrNot": "N",
"CheckList": [
{
"id": "123"
},
{
"id": "234"
}
]
}'
When I run the Curl command, I could get:
400 Bad Request.
But when I try to use Ruby to make the request like below:
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
I will always get a
502 Bad Gateway: an invalid response was received from the upstream server\n.
Could anyone explain why this happened? How could I fix it? And is there a better way to implement a Curl Patch requirement in Ruby?