2017-09-14 94 views
0

Ruby的Net::HTTP::Post似乎覆盖自定义Content-Type标题。当我设置页眉'Content-Type':'application/json'我收到以下错误从服务器:Ruby Net :: HTTP :: Post覆盖自定义内容类型标题

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 

回答