ruby http error - wrong Content-Length format
I have the following code
url = URI.parse("http://localhost:3100/tasks/#{id}.xml")
http = Net::HTTP.new(url.host, url.port)
# Make request
response = http.start do |http|
http.get(url.request_uri)
end
Whenever this code is run, it returns the following error
Net::HTTPHeaderSyntaxError in TasksController#show
wrong Content-Length form开发者_运维技巧at
Does anyone know whats going wrong here?
Thanks, Josh
Hmm, this code does not look right:
response = http.start do |http|
The variable you are passing to your block should not have the same name as an existing variable. You may want something more like what is in the docs.
require 'net/http'
url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res.body
Your code works when I point it to, for example, http://www.google.com, so I suspect that your server is spitting out something that gives Net::HTTP indigestion. Try adding this line just after your call to HTTP.new:
http.set_debug_output($stderr)
That should spew the entire transaction to the screen. I imagine you'll find something odd with the Content-Length field in the response header (Net::HTTP expects to find a string of digits in the that field). If it's not obvious from that what's the matter, then post that output (or at least the header portion of the response) here and let these smart guys have a go at it.
精彩评论