【问题标题】:How to send header and cookies with rest-client gem如何使用 rest-client gem 发送标头和 cookie
【发布时间】:2015-08-06 21:01:05
【问题描述】:

在代码中,我试图在同一个请求中同时发送标头和 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)):

【问题讨论】:

    标签: ruby-on-rails ruby cookies rest-client


    【解决方案1】:

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

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

    initializehttps://github.com/rest-client/rest-client/blob/master/lib/restclient/request.rb中的方法

    这样做 -

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

    【讨论】:

      【解决方案2】:

      试试

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多