'http error 400 with esp8266 when trying to send a JSON objekt
Hej I wish to send a json objekt from my esp8266 to a SQL database which works when i test it with postman. The problem occurs when I try sending it from esp8266 then i receive a http 400 error, but I dont understand why because i used the same objekt that I used in Postman and I also use the same URL namely http://daweatech.dk/api/measurements. My code can be seen below,
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
const char* ssid = "Pedersensnet";
const char* password = "";
String output;
//Your Domain name with URL path or IP address with path
const char* serverName = "http://daweatech.dk/api/measurements";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
output = "{\"timeStart\":\"2022-05-03T09:00:00\",\"timeEnd\":\"2022-05-03T10:00:00\",\"unit\":\"kWh\",\"measurement\":0.024,\"energyType\":\"Electric\",\"meterId\":\"123456789\"}";
Serial.println(output);
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// If you need an HTTP request with a content type: application/json, use the following:
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(output);
// If you need an HTTP request with a content type: text/plain
//http.addHeader("Content-Type", "text/plain");
//int httpResponseCode = http.POST("Hello, World!");
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// Free resources
http.end();
}
void loop() {
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|