Setting Request Headers in Ruby

后端 未结 4 1890
忘掉有多难
忘掉有多难 2021-01-31 16:32

I have the rest client gem and I am defining a request like this:

url = \'http://someurl\'
request =  {\"data\" => data}.to_json
response = RestClient.post(ur         


        
相关标签:
4条回答
  • 2021-01-31 16:36

    I had the same problem with Rest-Client (1.7.2) I need to put both params and HTTP headers.

    I solved with this syntax:

    params = {id: id, device: device, status: status}
    headers = {myheader: "giorgio"}
    
    RestClient.put url, params, headers
    

    I hate RestClient :-)

    0 讨论(0)
  • 2021-01-31 16:37

    The third parameter is the headers hash.

    You can do what you want by:

    response = RestClient.post( 
      url, 
      request,
      :content_type => :json, :accept => :json, :'x-auth-key' => "mykey")
    
    0 讨论(0)
  • 2021-01-31 16:55

    You can also do this

    RestClient::Request.execute(
       :method => :get or :post,
       :url => your_url,
       :headers => {key => value}
    )
    
    0 讨论(0)
  • 2021-01-31 16:55

    If PUT isn't allowed we can pass it in the header of POST. Headers in bold. This worked for me:

    act_resp = RestClient.post url, req_param, **:content_type => :json, :method => :put**

    0 讨论(0)
提交回复
热议问题