内容类型与HTTParty对Rails的麻烦

时间:2022-02-27 00:17:01
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....

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

ideas?

2 个解决方案

#1


13  

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:

尝试添加Accept:

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

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

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

#2


0  

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

HTTParty.post( ,:body =>“这是正文”,:headers => {“Content-Type”=>“text / html”})

Same as:

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

#1


13  

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:

尝试添加Accept:

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

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

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

#2


0  

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

HTTParty.post( ,:body =>“这是正文”,:headers => {“Content-Type”=>“text / html”})

Same as:

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