如何使用rest-client gem发送头部信息和cookies

4

我正在编写代码尝试在同一请求中发送头部和 cookie,以下是代码:


 @result = RestClient.post(
              'url',
              {:billingSourceCode => "code"},
              {:cookies => {:session_id => "1234"}},
              {:headers => {'Content-Type' =>'application/json', 
                           "Authorization" => "key",  
                           "Accept" => "application/json"}})

我收到下面的错误信息:
ArgumentError (wrong number of arguments (4 for 3)):
2个回答

1

Cookie是头部的一部分。在这里,RestClient

    @cookies = @headers.delete(:cookies) || args[:cookies] || {}

请查看 https://github.com/rest-client/rest-client/blob/master/lib/restclient/request.rb 中的 initialize 方法。

执行以下步骤 -

@result = RestClient.post(
          'url',
          {:billingSourceCode => "code"},
          {:headers => {'Content-Type' =>'application/json', 
                       "Authorization" => "key",  
                       "Accept" => "application/json"},
                       {:cookies => {:session_id => "1234"}}
          })

0

尝试一下

RestClient::Request.execute(
      method: :post,
      url: 'whatever',
      cookies: {:session_id => "1234"},
      headers: {'Content-Type' =>'application/json',
                      "Authorization" => "key",
                      "Accept" => "application/json"})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接