element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
RoadTests & Reviews
  • Products
  • More
RoadTests & Reviews
RoadTest Forum Microchip AVR-IoT question: How to correctly format a POST request
  • Blogs
  • RoadTest Forum
  • Documents
  • RoadTests
  • Reviews
  • Polls
  • Files
  • Members
  • Sub-Groups
  • More
  • Cancel
  • New
Join RoadTests & Reviews to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 9 replies
  • Answers 1 answer
  • Subscribers 2568 subscribers
  • Views 4588 views
  • Users 0 members are here
  • REST Post
Related

Microchip AVR-IoT question: How to correctly format a POST request

ischonfeld
ischonfeld over 3 years ago

I've been working with the Microchip mini dev board in the Arduino environment [Side note, very nice board so far]. I am converting a program that currently runs using Wifi to this board. It does a POST to a service called Scriptr. The original stripped down code that does the POST is:

  // Working code for Wifi (stripped of error checking and other extraneous stuff)
  const char* ssid     = "myssid";
  const char* password = "mypw";
  String mymsg = "TestMSG";
  const char* bearer    = "xxxxx...xxxxx";  // API Key

  WiFiSSLClient client;

  WiFi.begin(ssid, password);
  client.connect("api.scriptrapps.io", 443)) 

  client.print(String("POST ") + "/SNDSMS/?sms=" + mymsg + " HTTP/1.1\r\n" +  
               "Host: api.scriptrapps.io\r\n" + 
               "Authorization:bearer " + bearer + "\r\n" + // 23 + 68
               "Content-type: application/x-www-form-urlencoded\r\n" +
               "Connection: close\r\n\r\n");
  

The corresponding code for the Microchip mini dev board that I've tried is:

  Lte.begin();
  HttpClient.configure(host, 443, true);   // Have tried true and false for TLS

  HttpResponse response;

  // Following doesn't work
  response = HttpClient.post("/SNDSMS/","?sms=TestMSG HTTP/1.1\r\nHost: api.scriptrapps.io\r\nAuthorization:bearer xxxxx...xxxxx\r\nContent-type: application/x-www-form-urlencoded\r\nConnection: close\r\n\r\n" ); 

  // Following doesn't work either
  response = HttpClient.post("/SNDSMS","/?sms=TestMSG HTTP/1.1\r\nHost: api.scriptrapps.io\r\nAuthorization:bearer xxxxx...xxxxx\r\nContent-type: application/x-www-form-urlencoded\r\nConnection: close\r\n\r\n" ); 

  // Following doesn't work either
  response = HttpClient.post("api.scriptrapps.io","/SNDSMS/?sms=TestMSG HTTP/1.1\r\nHost: api.scriptrapps.io\r\nAuthorization:bearer xxxxx...xxxxx\r\nContent-type: application/x-www-form-urlencoded\r\nConnection: close\r\n\r\n" ); 

I ran https_configure_ca so HTTPS should be enabled. 

I"ve tried numerous guesses at how to format the Post request for the board, all either result in a response of 400 or 0. 

The board is connecting just fine to AT&T and obviously sending the request to Scriptr or I wouldn't be getting 400 errors (at least some of the time). 

Can anyone point me to how I should be formatting the POST request so that it does what the Wifi version does?

Thank you, Ira. 

  • Sign in to reply
  • Cancel

Top Replies

  • Gough Lui
    Gough Lui over 3 years ago +2
    It appears the HttpClient library they provide is too simple to allow for this - you are configuring the headers on a POST request and not the post body. The code itself is pretty clear - if you examine…
  • Gough Lui
    Gough Lui over 3 years ago +2 suggested
    I have modified the version of the library to allow for the extra parameters and it seems to work. First of all, the sample test code: #include <Arduino.h> #include <http_client.h> #include <led_ctrl…
  • Gough Lui
    Gough Lui over 3 years ago +1
    Further to my previous comment - Actually, I may have spoken too soon - it is early morning where I am, but it seems that AT+SQNFPUT for POST or PUT does support an extra header line, see Page 70, as…
Parents
  • Gough Lui
    0 Gough Lui over 3 years ago

    It appears the HttpClient library they provide is too simple to allow for this - you are configuring the headers on a POST request and not the post body. The code itself is pretty clear - if you examine http_client.h, you will find it only supports the following functions (comments removed):

    #ifndef HTTP_CLIENT_H
    #define HTTP_CLIENT_H
    
    #include <Arduino.h>
    #include <stdint.h>
    
    typedef struct {
        uint16_t status_code;
        uint32_t data_size;
    } HttpResponse;
    
    class HttpClientClass {
    
      private:
        HttpClientClass(){};
    
      public:
        static HttpClientClass &instance(void) {
            static HttpClientClass instance;
            return instance;
        }
    
        enum StatusCodes {
            STATUS_OK = 200,
            STATUS_NOT_FOUND = 404,
            STATUS_INTERNAL_SERVER_ERROR = 500,
        };
    
        bool configure(const char *host, const uint16_t port, const bool enable_tls);
    
        HttpResponse post(const char *endpoint,
                      const uint8_t *buffer,
                      const uint32_t buffer_size);
    
        HttpResponse post(const char *endpoint, const char *messsage);
    
        HttpResponse put(const char *endpoint,
                     const uint8_t *buffer,
                     const uint32_t buffer_size);
    
        HttpResponse put(const char *endpoint, const char *message);
    
        HttpResponse get(const char *endpoint);
    
        HttpResponse head(const char *endpoint);
    
        HttpResponse del(const char *endpoint);
    
        int16_t readBody(char *buffer, const uint32_t buffer_size);
    
        String readBody(const uint32_t size = 256);
    };
    
    extern HttpClientClass HttpClient;
    
    #endif

    As a result, there is no way, using the HttpClient library at present, to actually send custom headers as it just doesn't accept them in the function.

    The modem itself does support using custom headers on HTTP/S requests under very specific commands - notably AT+SQNHTTPQRY which supports an extra_header_line argument up to 1500 bytes (not sure if you can tack on \r\n to break that up into a few extra ones) but only for GET, HEAD or DELETE. This is not supported by AT+SQNFGET or ST+SQNFGETDATA. [EDIT - see my follow-up comment regarding POST - there are commands that support it on the modem side.]

    I can't copy the actual AT command data as the command reference may be under NDA (I obtained it as such), but an open copy seems to be available at Microchip - https://iot.microchip.com/docs/assets/files/ATCommandsLR8.0-NoCopyright-f9315d856c8b84593a1cf8391b904c36.pdf on Page 76. If you could change the method to GET, then some library modification may make it work.

    The only other option is similar to your previous code by using AT+SQNSD to do a raw socket connection and "spew" the request directly into the socket, but it will require an AT+SQNSSCFG for TLS security after the socket comes up which suggests it needs to be done asynchronously followed by an ATO to drop into data mode. Of course, working directly with AT commands with the module is not something that has been clearly documented at this time and while sequans_controller may help, it also isn't something that's clearly documented (especially handling unsolicited AT response codes in asynchronous set-up mode).

    Unfortunately, there are no easy answers for this one ... but you can always play with AT command sequences by loading the UART Bridge firmware (https://iot.microchip.com/docs/arduino/debugging/overview) and opening a terminal emulator (e.g. Arduino's Serial Monitor) and hand-entering command sequences until it seems to do what you want. At least you can debug the modem commands this way.

    - Gough

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Gough Lui
    0 Gough Lui over 3 years ago

    It appears the HttpClient library they provide is too simple to allow for this - you are configuring the headers on a POST request and not the post body. The code itself is pretty clear - if you examine http_client.h, you will find it only supports the following functions (comments removed):

    #ifndef HTTP_CLIENT_H
    #define HTTP_CLIENT_H
    
    #include <Arduino.h>
    #include <stdint.h>
    
    typedef struct {
        uint16_t status_code;
        uint32_t data_size;
    } HttpResponse;
    
    class HttpClientClass {
    
      private:
        HttpClientClass(){};
    
      public:
        static HttpClientClass &instance(void) {
            static HttpClientClass instance;
            return instance;
        }
    
        enum StatusCodes {
            STATUS_OK = 200,
            STATUS_NOT_FOUND = 404,
            STATUS_INTERNAL_SERVER_ERROR = 500,
        };
    
        bool configure(const char *host, const uint16_t port, const bool enable_tls);
    
        HttpResponse post(const char *endpoint,
                      const uint8_t *buffer,
                      const uint32_t buffer_size);
    
        HttpResponse post(const char *endpoint, const char *messsage);
    
        HttpResponse put(const char *endpoint,
                     const uint8_t *buffer,
                     const uint32_t buffer_size);
    
        HttpResponse put(const char *endpoint, const char *message);
    
        HttpResponse get(const char *endpoint);
    
        HttpResponse head(const char *endpoint);
    
        HttpResponse del(const char *endpoint);
    
        int16_t readBody(char *buffer, const uint32_t buffer_size);
    
        String readBody(const uint32_t size = 256);
    };
    
    extern HttpClientClass HttpClient;
    
    #endif

    As a result, there is no way, using the HttpClient library at present, to actually send custom headers as it just doesn't accept them in the function.

    The modem itself does support using custom headers on HTTP/S requests under very specific commands - notably AT+SQNHTTPQRY which supports an extra_header_line argument up to 1500 bytes (not sure if you can tack on \r\n to break that up into a few extra ones) but only for GET, HEAD or DELETE. This is not supported by AT+SQNFGET or ST+SQNFGETDATA. [EDIT - see my follow-up comment regarding POST - there are commands that support it on the modem side.]

    I can't copy the actual AT command data as the command reference may be under NDA (I obtained it as such), but an open copy seems to be available at Microchip - https://iot.microchip.com/docs/assets/files/ATCommandsLR8.0-NoCopyright-f9315d856c8b84593a1cf8391b904c36.pdf on Page 76. If you could change the method to GET, then some library modification may make it work.

    The only other option is similar to your previous code by using AT+SQNSD to do a raw socket connection and "spew" the request directly into the socket, but it will require an AT+SQNSSCFG for TLS security after the socket comes up which suggests it needs to be done asynchronously followed by an ATO to drop into data mode. Of course, working directly with AT commands with the module is not something that has been clearly documented at this time and while sequans_controller may help, it also isn't something that's clearly documented (especially handling unsolicited AT response codes in asynchronous set-up mode).

    Unfortunately, there are no easy answers for this one ... but you can always play with AT command sequences by loading the UART Bridge firmware (https://iot.microchip.com/docs/arduino/debugging/overview) and opening a terminal emulator (e.g. Arduino's Serial Monitor) and hand-entering command sequences until it seems to do what you want. At least you can debug the modem commands this way.

    - Gough

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube