【发布时间】:2016-01-30 05:12:35
【问题描述】:
我一直收到一个
400 "Bad Request" (Net::HTTPServerException)
每当我尝试从各种方法添加内容类型标头时都会出错。
我见过几个不同的例子,但我都无法工作。我的目标是在我的请求中添加 JSON 的内容类型。没有标头,我的请求不会出错:
def post_data(notice)
uri = URI('my uri is here')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}")
text = notice
req.add_field('Content-Type', 'application/json')
req.body = "{\"sensu_payload\" = #{payload(text).to_json}}"
response = http.request(req)
verify_response(response)
end
我也试过这种添加标题的方法:
request = Net::HTTP::Post.new(@path, initheader = {'Content-Type' =>'application/json'})
【问题讨论】:
标签: ruby json header content-type