-->
Page 1 of 1

Get request result

PostPosted: Wed Jun 13, 2018 1:40 pm
by johweb
Hello,
New to esp8266, using arduino ide, I'm trying to make a get request but it never success... Here's my code :
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>

/* Set these to your desired credentials. */
const char *ssid = "ssid";
const char *password = "password";

const char *host = "api.coinmarketcap.com";

void setup() {
  delay(1000);
  Serial.begin(115200);
  WiFi.mode(WIFI_OFF);
  delay(1000);
  WiFi.mode(WIFI_STA);
 
  WiFi.begin(ssid, password);
  Serial.println("");

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  delay(15000);

  Serial.print("connecting to ");
  Serial.println(host);
 
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  String url = "/v1/ticker/iota/?";
 
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);
 
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
}

I don't understant, I have no error... Thanks for your help !

Re: Get request result

PostPosted: Fri Jun 15, 2018 11:29 am
by martinayotte
This "api.coinmarketcap.com" site seems to redirect to HTTPS, so, a plain TCP connect won't work.
Use the HTTPSRequest example instead ...

Re: Get request result

PostPosted: Fri Jun 15, 2018 11:07 pm
by johweb
Oh... I'm sorry it was really a newbie question !
Yes, I'm now using WifiClientSecure library and calling 443 port. It's working fine, now !