Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby Rest客户端文章,如何发送头_Ruby On Rails_Ruby_Rubygems_Ruby On Rails 5_Ruby 2.0 - Fatal编程技术网

Ruby on rails Ruby Rest客户端文章,如何发送头

Ruby on rails Ruby Rest客户端文章,如何发送头,ruby-on-rails,ruby,rubygems,ruby-on-rails-5,ruby-2.0,Ruby On Rails,Ruby,Rubygems,Ruby On Rails 5,Ruby 2.0,Ruby rest客户端无法发送头,当我触发Ruby的post请求时,我的java服务无法读取头,如下所示。My Service layers抛出错误标头companyID丢失。当在Postman中运行相同的http请求时,它可以工作 response = RestClient::Request.new({ method: :post, url: 'https://example.com/submitForm', headers:{content_type:

Ruby rest客户端无法发送头,当我触发Ruby的post请求时,我的java服务无法读取头,如下所示。My Service layers抛出错误标头companyID丢失。当在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
这是有效的邮递员代码。需要类似的红宝石休息,请建议

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

尝试使用RestClient post方法:

result = RestClient.post(
  'https://example.com/submitForm', 
  payload, 
  {
    content_type: 'application/x-www-form-urlencoded', 
    companyID: 'Company1', 
    Authorization: 'Basic HELLO1234'
  }
)
本例中的有效负载是一个字符串,因此您需要为application/x-www-form-urlencoded找到合适的结构。例如:

payload.to_json => '{"data": "str_res"}'

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

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

应该是
公司ID
?您有
companyID
(看起来可能区分大小写),这只是问题中的一个输入错误,请更正。还需要帮助吗