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 Controller 中,标题位于ContentType text/html中。所以很明显我的标题参数不起作用...
想法?
最佳答案
尝试这种方式:
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。
关于ruby-on-rails - HTTParty针对Rails的内容类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11516241/