Paahn
Paahn

Reputation: 21

Request using HTTParty with API key

I am trying to make a GET request to this API, which works fine in POSTMAN. In my app I am using HTTParty to make the same request, and I am failing to structure the request correctly, resulting in a {"statusCode"=>404, "error"=>"Not Found", "message"=>"Not Found"} coming back. I've tried many variations but havent found the correct structure or naming of the parameters. This is for reference, where ENV["X-CMC_PRO_API_KEY"] is the enviroment variable holding my api key.

url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest/'

request = HTTParty.get(url,
  {
    headers: {
    "key": ENV["X-CMC_PRO_API_KEY"],
    "Accept": "application/json"
    },
    data: {
      id: 1
    },
    params: {
      slug: self.slug
    }
  }
)
response = JSON.parse(request.body)

this is how the API documentation suggests a cURL should be

curl -H "X-CMC_PRO_API_KEY: apikey" -H "Accept: application/json" -d "id=1" -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest

these are the screens from POSTMAN, where the request is returning the correct response. image1 image2

I appreciate any input on helping me with this request. I am a newbie and the HTTParty docs have not been helpful, nor have I found any other similar examples.

Upvotes: 1

Views: 1335

Answers (1)

Paahn
Paahn

Reputation: 21

This worked for me

headers = {
  'X-CMC_PRO_API_KEY' => "xxxxxxxxxxxxxxxxxxxxx"
}

url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'
params = '?&slug=bitcoin'
response = HTTParty.get(url+params,
  :headers => headers
)

Upvotes: 1

Related Questions