rubynet-http

Posting with headers


I have simple post using net/http.

uri = URI("https://example.com")
params = {title: 'foo', body: 'bar'}
Net::HTTP.post_form(uri, params)

Now, I need to add a header, but I can't find a way to make it work. Any ideas?

I tried something like

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(params)
request["Content-Type"] = "application/json"
response = http.request(request)

and

req = Net::HTTP::Post.new(uri)
req.set_form_data(params)
request["Content-Type"] = "application/json"
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
  http.request(req)
end

But I always get lib/ruby/gems/3.2.0/gems/net-protocol-0.2.2/lib/net/protocol.rb:237:in rbuf_fill': end of file reached (EOFError)


Solution

  • You haven't set the request.use_ssl = true. Take a look at this post, it's the same problem i think. EOFError: end of file reached issue with Net::HTTP