How to get data from IEX cloud

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char* ssid = "";
const char* password = "";

//Your Domain name with URL path or IP address with path
String serverName = "https://cloud.iexapis.com/stable/stock/psth/quote/latestPrice?token=";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200); 

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName; + "?format";
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

I am attempting to use this to get data from IEX cloud although I get an error code: -1 which I assume is from this code itself. When I paste the IEX url into a browser it returns a blank website with a value in a

 tag. How do I extract this pre value? I'm not sure if this code could work or if there is a better method. I am using an ESP8826

are you sure "https://cloud.iexapis.com" is valid?
i get "not found" when i try reaching it with a browser

gcjr:
are you sure "https://cloud.iexapis.com" is valid?
i get "not found" when i try reaching it with a browser

I suspect that is because that is the base address, if you try https://cloud.iexapis.com/v1/ you will get some data.

If I try "https://cloud.iexapis.com/stable/stock/psth/quote/latestPrice?token=?format"
I get a "400 Bad Request" rather than a blank page though.

Sorry I forgot to mention that there is a private api key after token=

gcjr:
are you sure "https://cloud.iexapis.com" is valid?
i get "not found" when i try reaching it with a browser

There is a private api key after token=

So is the code you have posted different to the code you are actually using? If so can you post the code you are actually using - change any private data such as the key, but keep the same format size etc.

countrypaul:
So is the code you have posted different to the code you are actually using? If so can you post the code you are actually using - change any private data such as the key, but keep the same format size etc.

exact same code, minus wifi ssid, wifi password and api key

I would expect to see an ampersand (&) in front of format, not a question mark (?).

wildbill:
I would expect to see an ampersand (&) in front of format, not a question mark (?).

No that is right, referenced the api docs at IEX Cloud Legacy API | IEX Cloud

What error code are you actually getting? How are you passing the api key as there is nothing in your code to show it being used, Just use an invalid string such as xxx in the same format as the real key to show how it is passed.

countrypaul:
What error code are you actually getting? How are you passing the api key as there is nothing in your code to show it being used, Just use an invalid string such as xxx in the same format as the real key to show how it is passed.

https://cloud.iexapis.com/stable/stock/psth/quote/latestPrice?token=apikey?format the api key is in the url

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.