![](/img/trans.png)
[英]Is the Net::HTTP Ruby gem ignoring the Content-type header in my HTTP responses?
[英]Ruby Net::HTTP::Post overrides custom Content-Type header
似乎Ruby的Net::HTTP::Post
覆蓋了自定義Content-Type標頭。 當我設置標題'Content-Type':'application/json'
我從服務器收到以下錯誤:
HTTP Status 400 - Bad Content-Type header value: 'application/json, application/x-www-form-urlencoded'
注意application/x-www-form-urlencoded
。 為什么在那兒? 有沒有辦法將其刪除?
我的代碼:
def post(uri, params)
req = Net::HTTP::Post.new(uri.path, 'Content-Type':'application/json')
req.form_data = params
Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
end
有趣的是,以下代碼使用Net::HTTP
以不同的方式工作:
def post(uri, params)
headers = {'Content-Type' =>'application/json'}
request = Net::HTTP.new(uri.host, uri.port)
request.post(uri.path, params.to_json, headers)
end
原因是在您顯式設置request.form_data
的第一個代碼段中:
req.form_data = params
表單使用x-www-urlencoded
隱式x-www-urlencoded
。
可能最好在Net::HTTP::Post.new
設置body
或顯式設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.