Question

I want to translate a curl request to a Typhoeus request. Curl request:

@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:

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}"}')

When I run the Typhoeus request, I get a response saying unauthorized which I believe indicates my user info is incorrect. How can I set the -u flag equivalent in a Typhoeus request?

Was it helpful?

Solution

How can I set the -u flag equivalent in a Typhoeus request?

Use the userpwd key within the options hash, e.g:

url = "http://httpbin.org/basic-auth/john/pwd1234"
Typhoeus::Request.new(url, userpwd: "john:pwd1234").run
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top