Akshat Akshat - 7 months ago 25
Ruby Question

HTTP.post_form in Ruby with custom headers

Im trying to use Nets/HTTP to use

POST
and put in a custom user agent. I've typically used
open-uri
but it cant do
POST
can it?

I use

resp, data = Net::HTTP.post_form(url, query)


How would I change this to throw custom headers in?

Edit my query is:

query = {'a'=>'b'}

Answer

You can try this, for example:

http = Net::HTTP.new('domain.com', 80)
path = '/url'

data = 'form=data&more=values'
headers = {
  'Cookie' => cookie,
  'Content-Type' => 'application/x-www-form-urlencoded'
}

resp, data = http.post(path, data, headers)
Comments