简体   繁体   English

带有自定义标头和正文的Ruby POST?

[英]Ruby POST with custom headers and body?

I'm trying to POST to Mailchimp in Ruby but I can't get any code working which has custom headers and a body. 我正在尝试使用Ruby POST到Mailchimp,但是我无法使用具有自定义标头和正文的任何​​代码。 This is the request I am trying to replicate: 这是我要复制的请求:

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

but I also have to add a JSON body. 但我还必须添加一个JSON正文。

If I run this code: 如果我运行此代码:

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

I get a response from Mailchimp that the apikey is missing. 我收到Mailchimp的答复,提示缺少apikey。 How do I add the API key? 如何添加API密钥?

Based on these posts: Ruby request to https - "in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)" 基于这些帖子: Ruby对https的请求-“在`read_nonblock'中:对等方重置连接(Errno :: ECONNRESET)”

Ruby send JSON request Ruby发送JSON请求

I tried this: 我尝试了这个:

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}"

but I get the error TypeError (no implicit of Hash into String) on the response = ... line. 但是我在response = ...行上收到错误TypeError (no implicit of Hash into String) What is the error referring to and how do I fix it? 错误指的是什么,我该如何解决?

UPDATE: 更新:

using start instead of new : 使用start而不是new

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

I am able to send the request, but I get a 400 response: "We encountered an unspecified JSON parsing error" 我能够发送请求,但得到400响应:“我们遇到未指定的JSON解析错误”

I get the same response with the posted answer. 我得到与发布答案相同的答复。 here is my JSON: 这是我的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' } )

but I get the same JSON parse error 但我得到相同的JSON解析错误

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

You need to set form data in post request like 您需要在post请求中设置表单数据,例如

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

Updates: 更新:

Try posting raw data like 尝试发布原始数据,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM