简体   繁体   English

HTTP 使用 ESP8266 获取请求

[英]HTTP get Request using ESP8266

Why I am unable to send get request.为什么我无法发送获取请求。 It always response 400.The URL are okay there is no problem in it.它总是响应 400。URL 没问题,没有问题。 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:以下是 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.我不知道如何解决这个问题。

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这里有一个(几个)示例如何使用 HTTPs 执行此操作: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClientino。

#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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM