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

在我的 rails 控制器中,标题来自 ContentType text/html。所以很明显我的标题参数不起作用....

想法?

4

2 回答 2

16

试试这种方式:

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

也尝试添加Accept

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

另外,检查它是否没有从 cookie 中获取选项 - 清理 cookie。

于 2012-07-17T05:01:07.007 回答
0

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

如同:

options = {:body => "Same body", :headers => {"Content-Type" => "text/html"}}`
HTTParty.post(<same link>, options)`
于 2012-07-17T05:56:48.280 回答