There's a fair bit incorrect here:
- You don't need to JSONify the data, but you need to set the correct JSON headers. A lot of people do this don't worry.
- The whole cookie thing comes after the request, not in it.
- Some missing brackets here swell.
Here's a working example:
!/usr/bin/env ruby
require "rubygems"
require "httparty"
include HTTParty
class TheSite
base_uri 'https://www.Thesite.com'
def initialize(u, p)
@data = {
:UserName => u,
:Password => p
}
res = self.class.post('Account/LogOn',
:body => @data,
:options => {
:headers => {
'ContentType' => 'application/json',
'Accept' => 'application/json',
'SecretKey' => res.headers['Set-Cookie']
}
}
)
if res.code == 200
@cookie = res.request.options[:headers]['Cookie']
# do stuff
end
end
end