0

EDIT: Thank you for the answer, also we had figured out how to do it Micropython too - I believe that it is relevant for the Arduino/ESP8266/ESP32 community to share this too.

https://github.com/46elks/46elks-getting-started/blob/master/code-examples/MicroPython/https_post_with_micropython.py

How should I include parameters from, to and message to http.post?

Is this the correct way to do it? http.POST("'from:+XXXXX','to:+XXXXX','message:Hej'");

#include <M5StickC.h>
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>

#include <HTTPClient.h>

#define USE_SERIAL Serial

WiFiMulti wifiMulti;

void setup() {

    USE_SERIAL.begin(115200);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    wifiMulti.addAP("YYYY", "ZZZZZ");

}

void loop() {
    // wait for WiFi connection
    if((wifiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;

        USE_SERIAL.print("[HTTP] begin...\n");
        // configure traged server and url

          http.begin("https://api.46elks.com/a1/sms");
          http.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
          http.setAuthorization("XXXXX", "YYYYYY");
          int httpCode = http.POST("'from:+XXXXX','to:+XXXXX','message:Hej'");

        USE_SERIAL.print("[HTTP] GET...\n");
        // start connection and send HTTP header
         httpCode = http.GET();

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }

    delay(10000);
}
1
  • what does the server expect? x-www-form-urlencoded format is name=value&nextname=nextvalue. so with = as name value separator and & as name-value pairs separator. Commented Apr 18, 2020 at 8:04

1 Answer 1

2

What format of message does the server expect?
As you send "x-www-form-urlencoded" it should probably look like:

 "from=+XXXXX&to=+XXXXX&message=Hej"

where the different parts are

 "param1=paramValue1&param2=paramValue2&param3=paramValue3"

 param .... Name of the expected parameter
 = .... divides the param pair
 paramValue .... Content/value of the parameter
 & .... links the param pairs

so in your case

 int httpCode = http.POST("from=+XXXXX&to=+XXXXX&message=Hej");
3
  • 1
    Note that, in x-www-form-urlencoded, the “+” character is used to encode a space. If you mean a literal “+”, you should encode it as “%2B”. Commented Apr 18, 2020 at 12:44
  • My friend and I figured out how to do this in micropython - can I make a question about it and answer it or should I post it here somehow? github.com/46elks/46elks-getting-started/blob/master/… Commented May 2, 2020 at 11:55
  • Place the link and short explanation as an EDIT to your question, this way everything is together and helps others Thank you Commented May 2, 2020 at 12:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.