0

Why I am unable to send get request. It always response 400.The URL are okay there is no problem in it. Actually I need to send data using get request. I have tried my best to find the solution but I didn't get any idea about it. Please anyone can help me with the code

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>//this header is used to send get request and see response
#include <WiFiClient.h>
const char* ssid = "myssid";
const char* password =  "mypswd";
String host_url="https://api-test.technofield.net";
String url1="/api/data?token=TEST_TOKEN_123&data=";
int httpPort=80;
void setup() {
 Serial.begin(9600);
WiFi.begin(ssid, password);     //Connect to my WiFi router
  Serial.println("");
  Serial.print("Connecting"); //initiall step just displaying connecting
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) { //the llop wiil execute till wifi is not connected
    delay(500);
    Serial.print(".");
  }

  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);// connected to given ssid
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
}


void loop(){
  if(WiFi.status()==WL_CONNECTED){ //we will perform action if wifi is connected
    HTTPClient http; //intialize object of HTTPclient which is importent
    String data="Sunil Kumar Yadav";
    //String token="TEST_TOKEN_123";
    url1=url1+data;
    //full APi url is created you can manipulate those above data and token vaiable.
    http.begin(host_url,httpPort,url1);//specify the request,begin the request 
    int httpCode=http.GET(); //Send the request
//    if (httpCode>0){ //lets see the response from server
//      String payload = http.getString();//store response on payload variable
//      Serial.print("==============");//just for seperating the wifi connection status and server response
//      Serial.println(payload);//print the response payload
//    }
  Serial.print(httpCode);
    http.end();//if there is begin there will be end ,it will Close the connection
  }
  delay(500); //send the request in every 2sec, you can change it according to your need
  //you can also specify the time so that it will be easy to identify on which time data is send
}

Following is the output:

301
==============<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

I dont know how to solve this.

  • host_url have to be domain name. And you are trying to mix port 80 and https. it wil of course work in case you started https service at port 80 but i doubt you did that. – Maxim Sagaydachny May 07 '21 at 06:09

1 Answers1

0

There's one (of several) example how to do this with HTTPs here: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino

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

const char* ssid = "myssid";
const char* password =  "mypswd";
String url = "https://api-test.technofield.net/api/data?token=TEST_TOKEN_123&data=";

void setup() {
  Serial.begin(115200);
  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() {
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClientSecure client;
client.setInsecure();
    
    HTTPClient https;
    String data = "Sunil%20Kumar%20Yadav";
    String fullUrl = url + data;
    Serial.println("Requesting " + fullUrl);
    if (https.begin(client, fullUrl)) {
      int httpCode = https.GET();
      Serial.println("============== Response code: " + String(httpCode));
      if (httpCode > 0) {
        Serial.println(https.getString());
      }
      https.end();
    } else {
      Serial.printf("[HTTPS] Unable to connect\n");
    }
  }
  delay(5000);
}
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198