Secure HTTP and two lines header

Hi team, I have done some projects with Arduino but is my first with HTTP post request, so forgive me if this is a very basic issue. I have been reading a lot of stuff on the forum/internet but haven't find anything to put me on the right path and therefore any help will be greatly appreciated.

So, I have two basic issues:

  • First I have to post to a secure server (https://...) and according to what I have read the HTTPclient library doesn't support https. So, how to deal with this limitation?
  • Secondly my header is a two line header like this
accept: application/json
Content-Type: application/json

but all the examples I have seen handle just one line like:
http.addHeader("Content-Type", "application/json");

How should I handle the headers then?

With my code as it is now returns a 400 response code!

http.addHeader("Content-Type", "application/json");

      // JSON data to send with HTTP POST
      String httpRequestData = "{\"password\": \"mypassword\",\"login\":\"mylogin\"}";
      Serial.println(httpRequestData);

      // Send HTTP POST request
      int httpResponseCode = http.POST(httpRequestData);

      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
        
      // Free resources
      http.end();

Many thanks in advance for any help.

Which exact board are we talking about? There are several implementation of that name.

This isn't complete anyway.

That adds one header line. You can call that method several times to add more.

That's not your code, that's a small excerpt. Post complete code!

@pylon many thanks for your prompt answer, please see remarks below

Which exact board are we talking about?

Sorry for not mentioning that in the original post. I'm using an AI-Thinker ESP32-CAM board since my end goal is to upload a picture to the service provider

This isn't complete anyway.

Not sure what is missing, as I said I'm new to this field...

You can call that method several times to add more.

Thanks for this useful hint

Post complete code!

Here it goes (already updated with two lines of headers as you explained...)

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "SigloXX";
const char* password = "";

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Serial.println();

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  WiFiClient client;
  HTTPClient http;
  // set the server
  http.begin(client, "https://mydomain.com/auth/login");     
 // set the headers
  http.addHeader("accept", "application/json");
  http.addHeader("Content-Type", "application/json");

  // JSON data to send with HTTP POST
  String httpRequestData = "{\"password\": \"myPassword\",\"login\": \"myLogin\"}";

  // Send HTTP POST request
  int httpResponseCode = http.POST(httpRequestData);

  Serial.print("HTTP Response code: ");
  Serial.println(httpResponseCode);
        
  // Free resources
  http.end();
    
}

void loop() {
}

Once again, many thanks for your willingness to help

The ESP32 library supports HTTPS.

At least the "Host" and "Content-Size" headers are missing, but the ESP32 library includes that automatically if you call it the way you do it.

Thanks for the clarifications @pylon, any suggestion regarding what can I try? Or at least to find out what part of the request is wrong...

Best

Find out what the service you're calling expects. As you're posting only dummy data we have no clue what service you're calling.
Try first to write the Accept-Header correctly (although header fields should be case-insensitive).

@pylon again many thanks for your answer. Will try as you said. Just FYI the service I'm calling is a private service provider and therefore I can't publish the URL. I guess I'm going to ask for their support to see what is being received in the server side.

Best!

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