ruby-on-rails - HTTParty针对Rails的内容类型问题

标签 ruby-on-rails json 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' } )

在我的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/

相关文章:

ruby-on-rails - 删除/销毁和编辑路由

ruby-on-rails - ruby 对象数组...或散列

ruby-on-rails - ruby on rails 我应该从 Controller 查询数据库吗

php - 使用jquery解析php发送的json

php - MySqL 表每一行的谷歌图表

ruby-on-rails - Rails + HTTParty : Expecting JSON data in the body

ruby-on-rails - http 方: `connect':连接被对等方重置 - SSL_connect (Errno::ECONNRESET)

ruby-on-rails - %i 或 %I 在 Ruby 中做什么?

c# - 使用 Newtonsoft.Json 反序列化

ruby - 为什么我使用 HTTParty 得到 IO::EAGAINWaitReadable?