0

I’ve got a sensor hooked up to an ESP8266, and I want to send the data to my Synology NAS chat server.

The following line of code works to send a message to the chat server. I could concatenate this string together with a new payload, but is there a better way to create and send it?

int httpResponseCode = http.POST("http://192.168.1.xxx:port/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=%22blahblahblah%22&payload={%22text%22:%22Does this work?%22}");

I’ve got a sensor hooked up to an ESP8266, and I want to send the data to my Synology NAS chat server.

The following line of code works to send a message to the chat server. I could concatenate this string together with a new payload, but is there a better way to create and send it?

int httpResponseCode = http.POST("http://192.168.1.xxx:port/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=%22blahblahblah%22&payload={%22text%22:%22Does this work?%22}");

I appreciate everyone's feedback and I'm sorry if this is question was a duplicate. The other question either didn't fit my needs, or I didn't know how to implement it (probably the case). I'm just tinkering with some electronics and a bit of programming for fun.

Here is a working solution that works by just concatenating the string:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

//wifi stuff
#define STASSID "ssid"
#define STAPSK "pw"

String ApiStart = "http://192.168.1.199:5000/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=";
String apiToken = "%22BigStringofStuff%22";
String PayloadHeader = "&payload={%22text%22:%22";
String PayloadFooter = "%22}";
String finalString = "";
String ChatMessage = "";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());

// post once
  if ((WiFi.status() == WL_CONNECTED)) {

    WiFiClient client;
    HTTPClient http;

    http.begin(client, ApiStart);
      // Add headers
      http.addHeader("Content-Type", "application/json");
      ChatMessage = "Dumb Message.";
      finalString = ApiStart + apiToken + PayloadHeader + ChatMessage + PayloadFooter;
        Serial.println(finalString);

      int httpResponseCode = http.POST(finalString);
      // Check the response
      if (httpResponseCode > 0) {
        Serial.printf("HTTP response code: %d\n", httpResponseCode);
        String response = http.getString();
        Serial.println("Response:");
        Serial.println(response);
      } else {
        Serial.printf("HTTP response code: %d\n", httpResponseCode);
      }

      // Close the connection
    http.end();
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}
rickrett
  • 1
  • 2
  • 1
    The difference between a `GET` and `POST` request is that the `GET` request encodes the payload as arguments in the URL, while `POST` sends it as part of the body. You send the payload as part of the URL, i.e. a normal `GET` request (but sent as a `POST`), not as the body. Doesn't the `http.POST` function take a second argument, which is the body? – Some programmer dude Aug 18 '23 at 04:16
  • The payload I’ve shown works, but the original web hook I was given had two: Payload={Title:”abc” Text:”def”} – rickrett Aug 18 '23 at 04:49
  • for POST request you should put the payload into the body of the request. now you have it as an URL parameter – Juraj Aug 18 '23 at 05:01
  • From the example [here](https://randomnerdtutorials.com/esp32-http-get-post-arduino/#http-post) it seems you should be using `http.begin(...)` to start the request, including setting URL and more; Optionally set one or more headers with `http.addHeader(...)`; Then call `http.POST(message_body)` to set the actual body and do the actual `POST` request,; And finally `http.end()` to finish. – Some programmer dude Aug 18 '23 at 05:49
  • In the duplicate, please see [this answer](https://stackoverflow.com/a/60343909/440558), not the one with the highest votes. – Some programmer dude Aug 18 '23 at 05:50

0 Answers0