内容类型与HTTParty对Rails的麻烦

phi*_*son 5 json ruby-on-rails httparty

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' } )
Run Code Online (Sandbox Code Playgroud)

在我的rails控制器中,标题是ContentType text/html.显然我的标题参数不起作用....

想法?

Hit*_*eeb 15

试试这种方式:

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

尝试添加Accept:

:headers => {
  'Content-Type' => 'application/json', 
  'Accept' => 'application/json'
}
Run Code Online (Sandbox Code Playgroud)

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

  • 不应该是''Content-Type'而不是''ContentType'`? (5认同)