ruby-on-rails - 使用 HTTParty 将 Content-Type 更改为 JSON

标签 ruby-on-rails salesforce httparty

我正在尝试使用 Ruby on Rails 与 Salesforce API 进行通信。我可以很容易地获取数据,但是我在将数据发布到服务器时遇到了问题。根据 Quinton Wall 的帖子,我正在使用 HTTParty:

https://github.com/quintonwall/omniauth-rails3-forcedotcom/wiki/Build-Mobile-Apps-in-the-Cloud-with-Omniauth,-Httparty-and-Force.com

但我似乎能够从 salesforce 服务器得到的只是我将正文提交为 html 的错误

{"message"=>"此资源不支持 'application/x-www-form-urlencoded' 的媒体类型", "errorCode"=>"UNSUPPORTED_MEDIA_TYPE"}

负责的代码如下所示:

require 'rubygems'
require 'httparty'

class Accounts
  include HTTParty
  format :json

  ...[set headers and root_url etc]

  def self.save
    Accounts.set_headers
    response = (post(Accounts.root_url+"/sobjects/Account/", :body => {:name => "graham"}.to_json))
  end
end

任何人都知道为什么应该将正文发布为 html 以及如何更改它以使其绝对以 json 格式发布,以便销售人员不会拒绝它?

任何帮助,将不胜感激。干杯

最佳答案

Content-Type header 需要设置为“application/json”。这可以通过插入 :headers => {'Content-Type' => 'application/json'} 作为要发布的参数来完成,即:

response = post(Accounts.root_url+"/sobjects/Account/", 
  :body => {:name => "graham"}.to_json,
  :headers => {'Content-Type' => 'application/json'} )

关于ruby-on-rails - 使用 HTTParty 将 Content-Type 更改为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6154176/

相关文章:

mysql - 简单的 Rails/MySQL 查询生成一个奇怪的结果

javascript - Rails 3.2 - 表单多次错误提交

ruby-on-rails - 使用范围来过滤 Rails 5 中的结果

html - 将页面分成两列并在 HTML 中对齐它们

php - 56 连接后从代理接收到 HTTP 代码 403?

ruby-on-rails - Mechanize 和 HTTParty 中的 "Hostname does not match the server certificate"错误

ruby-on-rails - ruby /Rails : How do you customize the mailer templates of Devise?

jquery - 我怎样才能让 jQuery post 与销售人员 WebToLead 一起工作

ruby-on-rails - 我如何处理 HTTParty 的错误?

ruby-on-rails - Ruby httparty 加载错误