HTTP.post_form in Ruby with custom headers

http, post, ruby, ruby-on-rails

Solution

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)

Problem

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'} ```

Original source