Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Sev D
#29258 So. To make it short, i have following REST API URL from import.io (example):

https://api.import.io/store/data/45a41a ... cErQ%3D%3D

pasting this into my browser returns something.

Now i want that something on my arduino. So i thoght i'd just make a regular GET Request.

Problem is, i don't get anything back from the server. I tried Port 80 and 443 (http and https) and all possible ways of spelling the GET request out (With https://..., without, etc.). I'm not even getting a 400 back, i just get back nothing.

So, what am I probably doing wrong? Anyone out there able to get this to work or point me in the right direction? Can't be that hard, can it?

Here is my code

Code: Select all/*
 *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
 *
 *  You need to get GUID and privateKey at data.sparkfun.com and paste them
 *  below. Or just customize this script to talk to other HTTP servers.
 *
 */

#include <ESP8266WiFi.h>

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

const char* host = "api.import.io";
const char* import_url = "https://api.import.io/store/data/45a41a18-7c02-4b95-bcce-9506cabc2459/_query?input/webpage/url=http%3A%2F%2Fwww.ikea.com%2Fus%2Fen%2Fsearch%2F%3Fquery%3Dchair&_user=315f6e84-8fd6-449f-ae60-6eccfb9a017e&_apikey=7C2or4Abyj9Hhk%2BzDPQtDcwby5szRxJksOQk2qy%2FrJkvA1F7C82JG2WcDII3ofwuip3BK16Y8JLShCxwHgcErQ%3D%3D";


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

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 443;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  // We now create a URI for the request
  String url = import_url;
 
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  Serial.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

 
  delay(10);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
}


Some information before you ask: I can do GET request to the data.sparkfun.com server and receive data from them (either 200 or 400 if i did something wrong)
User avatar
By igrr
#29260 You're connecting to an HTTPS server (which is HTTP over TLS) while sending plain HTTP request. The server expects to see TLS hello and gets some garbage (http request) instead. So your data is not even getting past the TLS layer to the HTTP server.

You should use http:// API and connect to port 80. Also you should only send URI after your HTTP verb, not the whole URL (take a look at WiFiClient example).
User avatar
By Sev D
#29344 Thank you @igrr, this helped me make the code i posted work. Strangely enough the api url i'm actually trying to use still doesn't work, even thought, if you open it in the browser it works like the Ikea example url. How is that possible?

Code: Select all/*
 *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
 *
 *  You need to get GUID and privateKey at data.sparkfun.com and paste them
 *  below. Or just customize this script to talk to other HTTP servers.
 *
 */

#include <ESP8266WiFi.h>

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

const char* host = "api.import.io";
//this one works
const char* import_url = "/store/data/45a41a18-7c02-4b95-bcce-9506cabc2459/_query?input/webpage/url=http%3A%2F%2Fwww.ikea.com%2Fus%2Fen%2Fsearch%2F%3Fquery%3Dchair&_user=315f6e84-8fd6-449f-ae60-6eccfb9a017e&_apikey=7C2or4Abyj9Hhk%2BzDPQtDcwby5szRxJksOQk2qy%2FrJkvA1F7C82JG2WcDII3ofwuip3BK16Y8JLShCxwHgcErQ%3D%3D";
//this one doesn't
//const char* import_url = "/store/connector/81f578f7-8b05-4e80-87ed-80b30fd85bf5/_query?input/webpage/url=http%3A%2F%2Fwww.codecheck.info%2Fessen%2Fmuesli_ceralien%2Fbircher_fruchtmuesli%2Fean_4316268338783%2Fid_796815%2FHaferflocken_zart.pro&_user=fedcd5b6-caee-49f7-959b-7aba844ef508&_apikey=fedcd5b6caee49f7959b7aba844ef508aa7b51fc50b7893e8152be82609f327cc718fcf7b2590262939d941e85ac330b9fff800d7bff5902c07c90b6b40396258dce93e6446635880de387bbb334071d";

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

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  // We now create a URI for the request
  String url = import_url;
 
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  /*Serial.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
               */

 
  delay(10);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
}