带有自定义标头和正文的Ruby POST? [英] Ruby POST with custom headers and body?

查看:144
本文介绍了带有自定义标头和正文的Ruby POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ruby POST到Mailchimp,但是我无法使用具有自定义标头和正文的任何​​代码.这是我要复制的请求:

I'm trying to POST to Mailchimp in Ruby but I can't get any code working which has custom headers and a body. This is the request I am trying to replicate:

curl --request GET \
--url 'https://<dc>.api.mailchimp.com/3.0/' \
--user 'anystring:<your_apikey>'

但是我还必须添加一个JSON正文.

but I also have to add a JSON body.

如果我运行以下代码:

postData = Net::HTTP.post_form(URI.parse('https://xxx.api.mailchimp.com/3.0/lists/xxx/members/'), { ... }})
puts postData.body

我从Mailchimp收到一个响应,提示缺少apikey.如何添加API密钥?

I get a response from Mailchimp that the apikey is missing. How do I add the API key?

基于这些帖子: 对https的Ruby请求-" in "read_nonblock":对等方重置连接(Errno :: ECONNRESET)"

Ruby发送JSON请求

我尝试过:

uri = URI('https://xxx.api.mailchimp.com/3.0/lists/xxxx/members/')
req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
req.basic_auth 'anystring', 'xxxx'
req.body = URI.encode_www_form({ ... }})
response = Net::HTTP.new(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https').start {|http| http.request(req) }
puts "Response #{response.code} #{response.message}:#{response.body}"

但是在response = ...行上出现错误TypeError (no implicit of Hash into String).错误指的是什么,我该如何解决?

but I get the error TypeError (no implicit of Hash into String) on the response = ... line. What is the error referring to and how do I fix it?

更新:

使用start代替new:

response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http| http.request(req) }

我能够发送请求,但得到400响应:我们遇到了未指定的JSON解析错误"

I am able to send the request, but I get a 400 response: "We encountered an unspecified JSON parsing error"

我得到与发布答案相同的答复.这是我的JSON:

I get the same response with the posted answer. here is my JSON:

{'email_address' => 'xxxx@gmail.com', 'status' => 'subscribed', 'merge_fields' => {'FNAME' => 'xxx', 'LNAME' => 'xxx' }

我还尝试过这样添加数据:

I also tried adding the data like this:

req.set_form_data('email_address' => 'xxxx@gmail.com', 'status' => 'subscribed', 'merge_fields' => {'FNAME' => 'xxx', 'LNAME' => 'xxx' } )

但我收到相同的JSON解析错误

but I get the same JSON parse error

推荐答案

尝试一下(如果它对您有用)

Try this, if it works for you

uri = URI('https://api.mailchimp.com/3.0/lists/xxxx/members/')

Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  req = Net::HTTP::Post.new(uri)
  req['Content-Type'] = 'application/json'
  req.basic_auth 'username', 'password'
  req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')

  response = http.request req # Net::HTTPResponse object
end

您需要在post请求中设置表单数据,例如

You need to set form data in post request like

req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')


更新:

尝试发布原始数据,例如

Try posting raw data like

json_data = {'from' => '2005-01-01', 'to' => '2005-03-31'}.to_json
req.body = json_data

这篇关于带有自定义标头和正文的Ruby POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆