6
require 'HTTParty'
require 'json'

@payload ={
    "email" => "phil@gmail.com",
    "token" => "mytokenstuff",
    "content" => "here is some content",
    "notification_type" => "1",
    "name" => "here is a name",
    "auto_action" => "true"
 }

response = HTTParty.post('http://localhost:3000/api/create.json', :body =>JSON.dump(@payload), :headers => { 'Content-Type' => 'application/json' } )

In my rails controller, the header is coming in ContentType text/html. So obviously my headers param isn't working....

ideas?

phil swenson
  • 8,564
  • 20
  • 74
  • 99

2 Answers2

16

Try it this way:

HTTParty.post(
  'http://localhost:3000/api/create.json', 
  :body => JSON.dump(@payload), 
  :headers => {
    'Content-Type' => 'application/json', 
  }
)

Try to add Accept too:

:headers => {
  'Content-Type' => 'application/json', 
  'Accept' => 'application/json'
}

Also, check that it is not grabbing the options from a cookie - clean the cookies.

Sven R.
  • 1,049
  • 17
  • 24
Sully
  • 14,672
  • 5
  • 54
  • 79
0

HTTParty.post(<some link>, :body => "This is the body", :headers => {"Content-Type" => "text/html"})

Same as:

options = {:body => "Same body", :headers => {"Content-Type" => "text/html"}}`
HTTParty.post(<same link>, options)`
Kashyap
  • 4,696
  • 24
  • 26