HTTP Post request

Hi there! I am having a huge problem using the Httpsclient-particle library. All I want to do is to POST a Json object to my server that is located on Heroku. I have tested everything on the backend and it works like it should. The only problem is that my code somehow does not work. I am repeatedly getting status 400.
This should be simple POST, so i really dont want to work with Webhooks or any thing like that.

For obvious reasons and I will change my host site to something else.

PS: I am using a Particle Photon.

#include <HttpClient.h>
#include "application.h"

 HttpClient http;  
 http_header_t headers[] = {  
    { "Content-Type", "application/json" },
    { "Host" , "https://www.myserver.com"},
    { NULL, NULL }   
 };
 
 http_request_t request;  
 http_response_t response; 

void setup(){
  Serial.begin(baudRate);
  Postrequest();
  
}

void loop(){
     //In this part I will calculate the BPM.. So its unnecessary to show you guys this part.

}
void Postrequest(){

   request.port = 80;
   request.path = "/api/something";  
   request.body = "{\"name\":\"personsname\", \"datetime\":\"02:00\", \"bpm\":\"90\"}";  
   http.post(request, response, headers);
   Particle.publish("METHOD", String(response.status)); 
   
}

So that's not really tha case

Since it's a HTTPS server you'd need a HTTPS connection which is not in the scope of the HTTPClient library nor is there native support on the devices.

However, you'd also need to set request.hostname (without the protocol prefix) and not only state the host in the headers.

1 Like

Do you have any suggestions how I should tackle this task? I have never done https request with particle before.
My backend is created with Node.js with simple endpoints and deployed on Heroku. I have searched everywhere for a solution. But can’t seem to find any :confused:

Appreciate the help :slight_smile:

I'd not go down that rabbit hole.
There are some libraries that tried to leverage secure connetions for user applications with Particle devices but they are (often) cumbersome to use and rather heavy on the memory of the device.
The simplest way will either be to change your server to accept HTTP or go for webhooks.

some links

1 Like

Sorry for not replying to this earlier: I have the Particle port of WolfSSL up and running with Amazon AWS IoT on about 150 devices now thanks to @cermak’s great effort on porting. As @ScruffR says - it’s a memory hog and it complicates things quite a bit, but it works really well.

WolfSSL chews up about 70k of your 128k app memory, but makes it possible to talk to AWS IoT directly. The WolfSSL port can talk to any TLS server directly via TCP and it handles the exchange of certificates and setup of communications quite fast. If you need anything above the TCP layer, you’ll just write that to fit your application.

4 Likes