1

I have an esp32 development board that I am trying to use to get Bitcoin price data using the Coinbase api.

On the Coinbase developers website, they give this example Curl command to get Bitcoin price data:

curl https://api.coinbase.com/v2/prices/spot?currency=USD

I put this into a command prompt on my computer and it works as expected. So I used this as a template for my HTTP get statement in my Arduino sketch.

if (!client.connect("api.coinbase.com",80)) {
  Serial.println("Connection failed.");
}
delay(100);
client.print("GET /v2/prices/spot?currency=USD HTTP/1.1\r\nHost: api.coinbase.com\r\n\n");

However, when sent from my esp32, I get a "301 Moved Permanently" error. But the new Location specified in the response is exactly where I was accessing in the first place!

HTTP/1.1 301 Moved Permanently
Date: Sun, 22 Aug 2021 22:15:50 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Sun, 22 Aug 2021 23:15:50 GMT
Location: https://api.coinbase.com/v2/prices/spot?currency=USD
Set-Cookie: __cf_bm=ea2a2031c3fa42b910632c627d2ed4c48824323d-1629670550-1800-ARKBVsdI9BWVOOTWmKXp+cMv0hcxMU4YAOR1JyHtH2Q49TEBkGuGNXLxSMIanzC7W8Ga9uXBCtdo+Th7Fgir8Jc=; path=/; expires=Sun, 22-Aug-21 22:45:50 GMT; domain=.coinbase.com; HttpOnly
X-Content-Type-Options: nosniff
Server: cloudflare
CF-RAY: 682f764a88f3a3eb-PIT

Why does this HTTP request work with Curl, but not when sent from the esp32?

1
  • 1
    HTTP != HTTPS... Aug 22, 2021 at 22:58

1 Answer 1

1

It's not an error, it's an HTTP redirect.

The HTTP request worked as expected on the ESP32. Curl automatically handles the redirect. Your homegrown HTTP implementation does not.

The Coinbase API requires you to use HTTPS and does not allow HTTP. This protects private information, like API keys and information the API may return. It also verifies that you're talking to the real Coinbase API server and not an impersonator.

You'll need to use port 443 and the WiFiClientSecure library instead of WiFiClient library. Or even better, you could use an HTTP client that already implements the HTTP protocol, like HTTPClient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.