Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do a HTTPS post on ESP8266 #5975

Closed
s153341 opened this issue Apr 11, 2019 · 8 comments
Closed

How to do a HTTPS post on ESP8266 #5975

s153341 opened this issue Apr 11, 2019 · 8 comments

Comments

@s153341
Copy link

s153341 commented Apr 11, 2019

I am trying to do a HTTPS post on ESP8266. It works in Postman where it has the following:
POST https_//api.dgs.com/ccs-int/v1/aaplication/json
Headers:
Content-Type: application/json
Authorization: xxxxxxx
Environment: digitalexploration

But I keep getting: 'HTTP/1.1 401 Unauthorized'.

Can someone please help me with how to build up the headers correctly in the HTTPS POST? And maybe also give a link to some documentation? I cant seem to find any...

My code is:

    
  const char* host = "api.dgs.com";
  const int httpsPort = 443;
  String url = "/ccs-int/v1/applicationdatalogging/1.0";
  String msg = "";
  
  WiFiClientSecure client;
  
  Serial.print("connecting to : '");
  Serial.print(host);
  Serial.println("'");
  Serial.printf("Using fingerprint '%s'\n", fingerprint);
  client.setFingerprint(fingerprint);
  
  client.connect(host, httpsPort);
  
  Serial.print("requesting URL: '");
  Serial.print(url);
  Serial.println("'");
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
  	"Host: " + host + "\r\n" +
  	"Connection: close\r\n" +
  	"Content-Type: application/json" +
  	"Authorization: Bearer " + authorization_code + "\r\n" +
  	"Content-Length: " + msg.length() + "\r\n" +
  	"\r\n" +
  	msg + "\r\n");
  
  Serial.println("request sent");
  unsigned long timeout = millis();
  while (client.available() == 0) {
  	if (millis() - timeout > 5000) {
  		Serial.println(">>> Client Timeout !");
  		client.stop();
  		return;
  	}
  }
@SarenT
Copy link
Contributor

SarenT commented Apr 17, 2019

I use following code (simplified):

#include <ESP8266HTTPClient.h>
HTTPClient http;
...
wifiClientSecure.loadCACert(caCertFile);
...
wifiClientSecure.connect(host, 443);
...
http.begin(client, host + url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // e.g.
int httpCode = http.POST(msg); // e.g. a=4&b=6&c=something

@JAndrassy
Copy link
Contributor

if you get an 401 error, you get a response from the server. SSL HTTP connection worked, only authorization_code has wrong value

@SarenT
Copy link
Contributor

SarenT commented Apr 18, 2019

I have to say, I am very confused with examples I see. Here are a few confusing answers:

  1. ESP8266 can use certificates, too weak for that can only do fingerprint check (I managed to do client and server authentication with both self signed and trusted authority signed certificate chains).
  2. BearSSL and documentation doesn't seem to work. e.g. Examples often use BearSSLX509List but I see X509List instead.
  3. How does it even work with good old loadPrivateKey, loadCertificate and loadCACert? Are they broken?
  4. I was able to make MQTT two way authentication but it was with axTLS. Now, I can't get it work with HTTPS (server authentication works though).

For me the main problem right now is to understand, how to correctly use current libraries.

@devyte
Copy link
Collaborator

devyte commented Apr 26, 2019

This is a "how do I..." type question. Closing.

@devyte devyte closed this as completed Apr 26, 2019
@jabernalv
Copy link

devyte, yes is a quesrtion "how do I...". This site is for THAT questions.
Why did you close?

@devyte
Copy link
Collaborator

devyte commented Nov 17, 2019

@jabernalv This is an issue tracker, meant for tracking ussues in the core hosted in the repo. It is NOT for "how do I..." type questions or general help requests. Please see issue POLICY doc and ISSUE_TEMPLATE instructions for more details.

@jabernalv
Copy link

@devyte Because of people like you this world is boring. You look like a lawyer, programmers are willing to help not follow absurd protocols.

@devyte
Copy link
Collaborator

devyte commented Dec 23, 2019

I am an Engineer, and I volunteer my free time to develop and maintain the core in this repository. In agreement with the repo owner when I was invited to be maintainer, I established a repo POLICY, and it has served well: the number of open issues has dropped from 1000+ when I arrived to 270 today, and is still on a downwards trend.
Maybe I make things a bit boring, but numbers prove the policy is being effective, and getting the repo under control is my primary interest.
As someone who shows 0 contributions in the last year, please beware of criticism against those who actually contribute to open source. Here in this repo we choose to develop, not to hold your hand. You're right that other programmers are willing to help, but such basic help isn't meant for this tracker.

@esp8266 esp8266 locked as off-topic and limited conversation to collaborators Dec 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants