Post request to HTTPS (Photon: panic - hard fault)

I am trying to post a request to a HTTPS server. The header arrives at the server, but the body doesn’t, the body content (provider, identifier, type) doesn’t show up in the header either. I guess im sending it the wrong way? As a result, i get back a bad gateway result, which i guess is causing a memory overload on the particle (panic - hard_fault)

Does anyone know how to solve this problem?

This is my code, which produces the bad gateway: panic - hard fault

unsigned char httpRequestContent[] = "POST %s HTTP/1.1\r\n"
"Content-Type: applcation/x-www-form-urlencoded\r\n"
"User-Agent: MatrixSSL/" MATRIXSSL_VERSION "\r\n"
"Host: xxxxx\r\n"
"projectid: xxxxx\r\n"
"webtoken: xxxxxx\r\n"
"provider=CUP"
"identifier=testcupaimee"
"type=EVENT"
"Accept: */*\r\n"
"Content-Length: %d\r\n\r\n%s";

I have the request working from postman : should like like this

var options = { method: 'POST',
 url: 'https://xxxxx',
 headers:
  { 'content-type': 'application/x-www-form-urlencoded',
    'postman-token': 'xxxxx',
    'cache-control': 'no-cache',
    webtoken: 'xxxxx',
    projectid: 'xxxxx' },
 form:
  { provider: 'CUP',
    identifier: 'testcupaimee',
    type: 'EVENT',
  }

Is MATRIXSSL_VESION defined as string or as number?
The way you are using it it has to be a string.

But if this is not the reason with just those two quotes we can’t know what your other code might be doing wrong.

1 Like

Thanks @ScruffR, I’ve had a look and that isn’t the problem. Here is the whole code, maybe you can find something in it?

#include <SparkJson.h>
#include <httpsclient-particle.h>

static int anomalyLed = D7;
static int heartbeatLed = D7;

const bool g_https_trace = true;  // This controls debug info print to Serial
const char host [] = "api.glowfi.sh";
const char ad_endpoint [] = "/storedata";
const char se_endpoint [] = "/storedata";
const int g_port = 443; 
static unsigned int freemem;
bool g_https_complete;
uint32 g_bytes_received;

TCPClient client;

#define GF_JSON_SIZE 300

unsigned char httpRequestContent[] = "POST %s HTTP/1.1\r\n"
 "Content-Type: applcation/x-www-form-urlencoded\r\n"
 "User-Agent: MatrixSSL/" MATRIXSSL_VERSION "\r\n"
 "Host: xxx\r\n"
 "projectid: xxx\r\n"
 "webtoken: xxx\r\n"
 "provider=CUP"
 "identifier=testcupaimee"
 "type=EVENT"
 "Accept: */*\r\n"
 "Content-Length: %d\r\n\r\n%s";

void setup() {
  if (g_https_trace) {
    Serial.begin(9600);
  }
  pinMode(anomalyLed, OUTPUT);
  httpsclientSetup(host, se_endpoint);
}

unsigned int nextTime = 0;    // Next time to contact the server
int g_connected;
void loop() {
  unsigned int t = millis();
  if (nextTime > t) return;
  StaticJsonBuffer<GF_JSON_SIZE> glowfishJson;
  JsonObject& top = glowfishJson.createObject();
  top["provider"] = (const char *) System.deviceID();
  top["identifier"] = (const char *) System.deviceID();
  JsonObject& data_set = top.createNestedObject("data_set");
  // TODO: I don't quite understand how much memory needs to get allocated
  //JsonObject& time = top.createNestedArray("time");
  JsonArray& freememJ = data_set.createNestedArray("freemem");
  freememJ.add(System.freeMemory());
  JsonArray& timeup = data_set.createNestedArray("timeup");
  timeup.add(t/1000);
  char jsonBuf[GF_JSON_SIZE];
  size_t bufsize = top.printTo(jsonBuf, sizeof(jsonBuf));
  g_connected = client.connect(host, g_port);
  if (!g_connected) {
    client.stop();
    // If TCP Client can't connect to host, exit here.
    return;
  }
  g_https_complete = false;
  g_bytes_received = 0;
  if (g_https_trace) {
    Serial.print("free memory: ");
    Serial.println(freemem);
  }
  int rc;
  httpsclientSetPath(se_endpoint);
  if ((rc = httpsClientConnection(httpRequestContent, bufsize, jsonBuf)) < 0) {
    // TODO: When massive FAIL
    if (g_https_trace) {
      Serial.print("httpsClientConnection Returned ");
      Serial.println(rc);
    }
    httpsclientCleanUp();
    // Blink an LED twice to indicate trouble
    digitalWrite(anomalyLed, HIGH);
    delay(500);
    digitalWrite(anomalyLed, LOW);
    delay(500);
    digitalWrite(anomalyLed, HIGH);
    delay(500);
    digitalWrite(anomalyLed, LOW);
    return;
  } else {
    // Blink an LED once to indicate success
    digitalWrite(heartbeatLed, HIGH);
    delay(250);
    digitalWrite(heartbeatLed, LOW);
  }
  client.stop();
  nextTime = millis() + 5000;
}

@aimee, did you ever get this issue resolved?

Am having similar issues trying to get library httpsClient working with POSTs (it works okay with GET).

Thanks!