ESP32 POST using "Fetch for Arduino" library

I am using an ESP32 to make the POST below up to the SensorPush API using the Fetch for Arduino library.

#include "recipes/WiFi.h"
#include "Fetch.h"

void setup() {
    Serial.begin(9600);
    connectWiFi("<SSID>", "<PASSWORD>");

    RequestOptions options;
    options.method = "POST";
    options.headers["accept"] = "application/json";
    options.headers["Content-Type"] = "application/x-www-form-urlencoded";
    options.body = "email=<EMAIL_ADDRESS>&password=<PASSWORD>";

    Response response = fetch("https://api.sensorpush.com/api/v1/oauth/authorize", options);

    // Printing response.
    Serial.println(response);
}

void loop() {

}

The response I get back looks like this in the serial monitor:

{
"ok": 0
"status": 412
"statusText": "Precondition Failed"
"body": "{"message":"Must provide email or apiId"}"
}

I have tested this out using Insomnia and Postman where it seems to work just fine. I'm struggling to find a way to see what options looks like in Response response = fetch("https://api.sensorpush.com/api/v1/oauth/authorize", options) when it's actually sent over the wire.

I have a feeling that something about my BODY is getting lost. Whatever it is, it's driving me nuts!! :exploding_head:

Are you sure that the body should be formatted like this? I would expect an API to accept the request in JSON format. E.g.,

options.headers["Content-Type"] = "application/json";
options.body = "{\"email\": \"<EMAIL_ADDRESS>\", \"password\": \"<PASSWORD>\"}";

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.