如何用Typhoeus中的curl用户标志(-u)表示内容?

时间:2013-08-09 06:50:56

标签: ruby-on-rails curl typhoeus

我想将curl请求转换为Typhoeus请求。卷曲请求:

@response = %x{curl -i https://track.customer.io/api/v1/customers/#{@client.id} \
        -X PUT \
        -H "Content-Type: application/json" \
        -u #{APP_CONFIG[:customerio][:api_key]}:#{APP_CONFIG[:customerio][:api_key]} \
        -d '{
          "email":"#{@client.email}",
          "name":"#{@client.name}",
          "id":"#{@client.id}",
          "created_at":"#{@client.created_at.to_time.to_i}"}'}

Typhoeus请求:

request = Typhoeus::Request.new(
    "https://track.customer.io/api/v1/customers/4", 
    ssl_verifypeer: false,
    method: :put,
    headers: {
        "Content-Type" => "application/json",
        "user_agent" => "#{APP_CONFIG[:customerio][:api_key]}:#{APP_CONFIG[:customerio][:api_key]}"},
    body: '{
    "email":"first@last.com", 
    "name":"Name is here",
    "id":"4",
    "created_at":"#{Time.now}"}')

当我运行Typhoeus请求时,我得到一条回复说unauthorized我相信这表示我的用户信息不正确。如何在Typhoeus请求中设置-u标志等效?

1 个答案:

答案 0 :(得分:1)

  

如何在Typhoeus请求中设置等效的-u标志?

在选项哈希中使用userpwd键,例如:

url = "http://httpbin.org/basic-auth/john/pwd1234"
Typhoeus::Request.new(url, userpwd: "john:pwd1234").run