How to send GET request with esp8266 - Page 1
Forum section: electronics / general

author: parasbhanot
author_rank: Contributor
timestamp: March 17, 2016, 01:02:05 pm
content: hello everyone,I am trying to GET a TalkBack Command using Arduino IDE but i am not getting any response on Serial Monitor . I have tested the following GET request with the Poster. it works perfectly.https://api.thingspeak.com/talkbacks/7441/commands/1145081?api_key=WSMCMVFUJBI6Z115Code:#include <ESP8266WiFi.h>const char* ssid = "xxxx";const char* password = "yyyy";const char* host = "api.thingspeak.com";// Use WiFiClient class to create TCP connectionsWiFiClient client;void setup() {Serial.begin(115200);delay(100);// We start by connecting to a WiFi networkSerial.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());}void loop() {Serial.print("connecting to ");Serial.println(host);if (client.connect(host,80)) { // "184.106.153.149" or api.thingspeak.comString postStr = "GET /talkbacks/7441/commands/1145081?api_key=WSMCMVFUJBI6Z115 HTTP/1.1\n";client.print(postStr);client.print("Host: api.thingspeak.com\n");client.print("Connection: close\n");}// Read all the lines of the reply from server and print them to Serialwhile(client.available()){char c = client.read();Serial.print(c);}client.stop();Serial.println();Serial.println("closing connection");delay(15000);}i think something is wrong with my GET requestWhat is the correct way to send Talkback GET request ?

author: obiwanjacobi
author_rank: Super Contributor
timestamp: March 17, 2016, 01:41:04 pm
content: That means the connect call fails.Put the url in with the host string? (I have no idea how that API works...)

author: borjam
author_rank: Unknown
timestamp: March 17, 2016, 02:35:20 pm
content: Quote from: parasbhanot on March 17, 2016, 01:02:05 pmTry adding an extra newline at the end. Either add an extra \n after Connection: close\n or, to make your intent more clear, add an extra client.print("\n")

author: linux-works
author_rank: Super Contributor
timestamp: March 17, 2016, 02:50:50 pm
content: perhaps it will work better with char* instead of String type?

author: parasbhanot
author_rank: Contributor
timestamp: March 17, 2016, 04:39:46 pm
content: Problem solvedi have to remove header from GET request and neglect one empty line to get the actual response.This is my final code :Code:#include <ESP8266WiFi.h>const char* ssid = "xxxxx";const char* password = "yyyyy";const char* host = "api.thingspeak.com";String TalkBackID ="7441";String TalkBackAPIKey ="WSMCMVFUJBI6Z115";String Command_ID ="1145081";String url = "/talkbacks/"+TalkBackID+"/commands/"+Command_ID+"?api_key="+TalkBackAPIKey;// Use WiFiClient class to create TCP connectionsWiFiClient client;void setup() {Serial.begin(115200);delay(100);// We start by connecting to a WiFi networkSerial.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.print("IP address: ");Serial.println(WiFi.localIP());}void loop() {Serial.print("connecting to ");Serial.println(host);if (client.connect(host, 80)) {Serial.println("Tcp Connection eastablished with thingSpeak API");}else {Serial.println("connection failed");return;}client.print(String("GET ") + url + "&headers=false" + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");delay(500); // apparently some timeout is needed hereString messageBody = "";while (client.available()) {String line = client.readStringUntil('\n');if (line.length() == 1) { //actual content starts after empty line (that has length 1)messageBody = client.readStringUntil('\n');break;}}Serial.print("Found a message, the message is: ");Serial.print(messageBody);client.stop();Serial.println();Serial.println("closing connection");Serial.println();delay(15000);}