I am trying to send a JSON call via URL encode but it does not seem to work?
My Json:
{"MultiRelay":{"relay":0,"on":true}}
Converted:
%7B%22MultiRelay%22%3A%7B%22relay%22%3A0%2C%22on%22%3Atrue%7D%7D
My call string:
http://192.168.178.59/json?data=%7B%22MultiRelay%22%3A%7B%22relay%22%3A0%2C%22on%22%3Atrue%7D%7D
The Multi-Relay should witch … but does not do?
So I can not just put this url in a browser?
I managed to put it in a post request:
void postJSON() {
// Your JSON data
String jsonData = "{\"MultiRelay\":{\"relay\":0,\"on\":true}}";
// Your server's IP address or domain
String serverIP = "192.168.178.59";
// Create an HTTPClient object
HTTPClient http;
// Your URL
String url = "http://" + serverIP + "/json";
// Send the POST request with JSON data in the body
http.begin(url);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(jsonData);
// Check for a successful POST request
if (httpCode > 0) {
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
} else {
Serial.println("Error in POST request");
}
http.end();
}
… that works!
Use /json/state as it is leaner.
How would that look like given that function I currently have:
void jsonRequest(String jsonData) {
HTTPClient http;
String url = baseUrl + "json";
// Send the POST request with JSON data in the body
http.begin(url);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(jsonData);
// Check for a successful POST request
if (httpCode > 0) {
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
} else {
Serial.println("Error in POST request");
}
http.end();
}
void loop() {
jsonRequest("{MultiRelay:{relay:1,on:false}}");
}
If I understand correctly instead of:
String url = baseUrl + "json";
… I write:
String url = baseUrl + "json/state";
The benefit being? It creates a smaller feedback response?