James Watson
James Watson

Reputation: 55

HTTPS Post Request with ESP8266

I'm trying to send an HTTPS POST request with a ESP8266. I can make the request just fine with python and cURL just when I try it with the ESP it doesn't work. A segment of my code is below

const char *host = "api.pushbullet.com";
const int httpsPort = 443;
const char fingerprint[] PROGMEM = "4C 70 C5 AE F3 30 E8 29 D1 9C 18 C6 2F 08 D0 6A A9 AA 19 0F";

Link = "/post";

httpsClient.print(String("POST ") + Link + " HTTP/1.1\r\n" +
               "Host: " + host + "/v2/pushes" + "\r\n" +
               "Access-Token: *************"+ "\r\n" +
               "Content-Type: application/json"+ "\r\n" +
               "Content-Length: 20"+ "\r\n" +
               "body: Hello World" + "\r\n\r\n");

Serial.println("request sent");

The request I'm trying to make is below. This works just fine in python

import requests

headers = {
    'Access-Token': '***********',
    'Content-Type': 'application/json',
}
data = '{"body":"Hello World","title":"Hi","type":"note"}'
response = requests.post('https://api.pushbullet.com/v2/pushes', headers=headers, data=data)

And in cURL:

curl --header 'Access-Token: **********' --header 'Content-Type: application/json' --data-binary '{"body":"Hello World","title":"Hi","type":"note"}' --request POST https://api.pushbullet.com/v2/pushes

When I make the request with the Arduino code it returns "Error 411 (Length Required)!!".

This is probably due to some stupid mistake I've made but if anyone could help me fix my Arduino code I'd be very grateful. Thanks

Upvotes: 1

Views: 7698

Answers (2)

Marcel Stör
Marcel Stör

Reputation: 23525

General advice: when you use cURL always use --verbose to see the full HTTP exchange.

In your case it should be

    httpsClient.print(String("POST ") + "/v2/pushes" + Link + " HTTP/1.1\r\n" +
                   "Host: " + host + "\r\n" +
                   "Access-Token: *************"+ "\r\n" +
                   "Content-Type: application/json"+ "\r\n" +
                   "Content-Length: 20"+ "\r\n" +
                   "body: Hello World" + "\r\n\r\n");

Note how

  • the path is "/v2/pushes/" plus identifier (Link in your case?)
  • Host is just "api.pushbullet.com"

Side notes:

  • make sure you send correct values for Content-Length
  • consider using HTTPClient if you are not confident with low-level HTTP programming

Upvotes: 1

hcheung
hcheung

Reputation: 4014

There are a few mistakes in your code.

  1. your http POST format are incorrect in a)host name, b)uri, and c)header/body separation;
  2. your http body is not a valid json object.

Here is an example on sending the http (without using String):

const char *host = "api.pushbullet.com";
const char *uri = "/post/v2/pushes/";
const char *body ="{\"body\": \"Hello World\"}";  // a valid jsonObject
    
char postStr[40];
sprintf(postStr, "POST %s HTTP/1.1", uri);  // put together the string for HTTP POST
    
httpsClient.println(postStr);
httpsClient.print("Host: "); httpsClient.println(host);
httpsClient.println("Access-Token: *************");
httpsClient.println("Content-Type: application/json");
httpsClient.print("Content-Length: "); httpsClient.println(strlen(body));
httpsClient.println();    // extra `\r\n` to separate the http header and http body
httpsClient.println(body);

Upvotes: 3

Related Questions