使用 HTTParty 将 Content-Type 更改为 JSON

发布于 2024-11-10 13:10:29 字数 994 浏览 5 评论 0原文

我正在尝试使用 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"=>"MediaType of '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 形式发布这样销售人员就不会拒绝它?

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

I am trying to use Ruby on Rails to communicate with the Salesforce API. I can fetch data easily enough but I am having problems posting data to the server. I am using HTTParty as per Quinton Wall's post here:

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

but all I seem to be able to get from the salesforce server is the error that I am submitting the body as html

{"message"=>"MediaType of 'application/x-www-form-urlencoded' is not supported by this resource", "errorCode"=>"UNSUPPORTED_MEDIA_TYPE"}

the responsible code looks like:

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

anyone have an idea why the body should be being posted as html and how to change this so that it definitely goes as json so that salesforce doesn't reject it?

Any help would be appreciated. cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2024-11-17 13:10:29

Content-Type 标头需要设置为“application/json”。这可以通过插入 :headers => 来完成{'内容类型' => 'application/json'} 作为 post 的参数,即:

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

The Content-Type header needs to be set to "application/json". This can be done by inserting :headers => {'Content-Type' => 'application/json'} as a parameter to post, ie:

response = post(Accounts.root_url+"/sobjects/Account/", 
  :body => {:name => "graham"}.to_json,
  :headers => {'Content-Type' => 'application/json'} )
南街九尾狐 2024-11-17 13:10:29

您必须将 Content-Type 标头设置为 application/json。我没有使用过 HTTParty,但看起来你必须做一些事情,比如

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

我有点惊讶格式选项不会自动执行此操作。

You have to set the Content-Type header to application/json. I haven't used HTTParty, but it looks like you have to do something like

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

I'm somewhat surpised that the format option doesn't do this automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文