你好!我目前正在尝试使用我的 ESP8266 将 JSON 正文中的一些值发送到数据库。
当我通过 Postman 发送正文时,一切都很好。我试图按照在线教程进行操作,但是当我实现我的端点、api 密钥和正文时,我得到了响应代码“-1”。
主板:LOLIN (WEMOS) D1 mini lite
代码:
代码:
全选#include
#include
#include
void setup() {
Serial.begin(115200); //Serial connec
tion
WiFi.begin("ssid", "pass"); //WiFi connection
while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
delay(500);
Serial.println("Waiting for connection");
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
StaticJsonBuffer<300> JSONbuffer; //Declaring static JSON buffer
JsonObject& JSONencoder = JSONbuffer.createObject();
JSONencoder["article"] = "3 mm";
JSONencoder["amount"] = 23;
char JSONmessageBuffer[300];
JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
Serial.println(JSONmessageBuffer);
HTTPClient http; //Declare object of class HTTPClient
http.begin("URL/endpoint"); //Specify request destination
http.addHeader("Content-Type", "application/json"); //Specify content-type header
http.addHeader("auth-key", "Bearer "); //Specify authorization header
int httpCode = http.POST(JSONmessageBuffer); //Send the request
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
} else {
Serial.println("Error in WiFi connection");
}
delay(300); //Send a request every 30 seconds
}
串口
代码:
全选Waiting for connection
{
"article": "3 mm",
"amount": 23
}
-1