使用 ESP8266 的 HTTP 获取请求

HTTP get Request using ESP8266

为什么我无法发送获取请求。它总是响应 400.The URL 没问题,没有问题。实际上我需要使用 get 请求发送数据。我已尽力找到解决方案,但我对此一无所知。请任何人都可以帮助我的代码

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

输出如下:

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

我不知道怎么解决。

这里有一个(几个)示例如何使用 HTTP 执行此操作: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);
}