Ruby Rest 客户端 POST,如何发送标头

Nag*_*aga 3 ruby rubygems ruby-on-rails ruby-2.0 ruby-on-rails-5

Ruby rest 客户端无法发送标头,当我触发来自 ruby​​ 的 post 请求时,我的 java 服务无法读取标头,如下所示。我的服务层抛出错误 Header companyID is missing。在 Postman 中运行相同的 http 请求时,它可以工作。

response = RestClient::Request.new({
      method: :post,
      url: 'https://example.com/submitForm',
      headers:{content_type: 'application/x-www-form-urlencoded',
              companyID:'Company1',
              Authorization:'Basic HELLO1234'},
      payload: { data: str_res}
    }).execute do |response, request, result|
      case response.code
      when 400
        [ :error, JSON.parse(response.to_str) ]
      when 200
        [ :success, JSON.parse(response.to_str) ]
        puts request.headers 
      else
        fail "Invalid response #{response.to_str} received."
      end
    end
Run Code Online (Sandbox Code Playgroud)

这是有效的邮递员代码。在 Ruby Rest 中需要类似的,请提供建议。

POST /submitForm HTTP/1.1
Host: example.com
companyID: Company1
Authorization: Basic HELLO1234
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: 528cafa1-2b5d-13a1-f227-bfe0171a9437

data=My Own data
Run Code Online (Sandbox Code Playgroud)

Nag*_*aga 7

下面工作。看起来标题应该在同一行。

payloadString = "data=My Own data" 

    response = RestClient::Request.new({
  method: :post,
  url: 'https://example.com/submitForm',
  payload: payloadString,
  headers: {content_type: 'application/x-www-form-urlencoded', companyID:'Company1', Authorization:'Basic HELLO1234'}
   }).execute do |response, request, result|
  case response.code
  when 400
    [ :error, JSON.parse(response.to_str) ]
  when 200
    [ :success, JSON.parse(response.to_str) ]
  else
    fail "Invalid response #{response.to_str} received."
  end
end
Run Code Online (Sandbox Code Playgroud)