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

HTTPS POST request to api returns -1 response (ssl) #2834

Closed
thorburn1 opened this Issue Jan 6, 2017 · 9 comments

Comments

Projects
None yet
4 participants
@thorburn1
Copy link

thorburn1 commented Jan 6, 2017

I am trying to post a .json message with field "chip_id" and "power" to my https api made. But response error code -1 is coming back, and data is not getting inserted there.
I used this code found and modified the url , wifi credentials ,and thumbprint.
Can someone explain the issue?
(working fine with postman)
(using standalone esp8266 in arduino ide )
----------------------------- Remove above -----------------------------

Basic Infos

Hardware

Hardware: ?ESP-12?
Core Version: ?2.1.0-rc2?

Description

Problem description

Settings in IDE

Module: Generic ESP8266 Module
Flash Size: 512k(64k SPIFFS)
CPU Frequency: 80Mhz
DEBUG LEVEL core+SSL+tlsMEM
debug port disabled
Flash Frequency: 40Mhz
Upload Using: ?OTA / SERIAL?
Reset Method: ck
arduino as isp

Sketch

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>

void setup() {

Serial.begin(115200);
WiFi.begin("iot", "oksurewhynot");

while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion

delay(500);
Serial.println("Waiting for connection");

}

}

void loop() {

if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status

HTTPClient http;
http.begin("https://fusmin.com/hooterstatus/api/HooterStatusUpdate.php" , "‎F6 XX XX XX XX XX XX 23 82 90 F4 BA D2 F5 30 AA FC B8 D3 7A");
http.addHeader("Content-Type", "application/json");
String postMessage = String("{'chip_id' : 'rohitesp', 'power' : '2'}");
int httpCode = http.POST(postMessage);
Serial.print("http result:");
Serial.println(httpCode);
http.writeToStream(&Serial);
String payload = http.getString();
http.end();

}else{

Serial.print("Error in Wifi connection");

}

delay(30000); //Send a request every 30 seconds

}

Debug Messages(edited)

Waiting for connection
ssl/tls1.c:545 malloc 6864, left 31360
please start sntp first !
State: sending Client Hello (1)
:sent 73
State: receiving Server Hello (2)
State: receiving Certificate (11)
crypto/bigint.c:1072 realloc 1032, left 26904
crypto/bigint.c:1072 realloc 1032, left 23320
crypto/bigint.c:1072 realloc 1032, left 19880
crypto/bigint.c:1072 realloc 1032, left 15920

State: receiving Server Hello Done (14)
crypto/bigint.c:1072 realloc 1024, left 12528
State: sending Client Key Exchange (16)
State: sending Finished (16)
:sent 69
State: receiving Finished (16)
pos:0 len:62 invalid hex sequence: â€
Alert: close notify
http result:-1

@vicnevicne

This comment has been minimized.

Copy link
Contributor

vicnevicne commented Jan 6, 2017

Well, the first error your log says is "[begin] failed to parse protocol".
And indeed, the URL you pass to begin does not start with a protocol ("http://"), so begin fails.
After that, connect complains that begin returned an error and so on, so I would suggest you to first fix the url by adding "http://" and retry...

@thorburn1

This comment has been minimized.

Copy link
Author

thorburn1 commented Jan 6, 2017

ok , so I edited my uri , but still http response is -1 , debug message i updated in the original post.
My thumbprint is correct, cross checked it from the certificate(sha1)

@vicnevicne

This comment has been minimized.

Copy link
Contributor

vicnevicne commented Jan 6, 2017

Wow, modifying your original code and log along the way is not very good practice because the discussion is now impossible to follow for others.
Anyway, I have zero experience with SSL on ESP, so I'm afraid it's now out of my field.
However, do you have success if using a GET method ? Just to know if it is really POST-related or if you have to look somewhere else...

@thorburn1

This comment has been minimized.

Copy link
Author

thorburn1 commented Jan 6, 2017

I think its something to do with my certificate instead ? code works fine with other api's out there , or with GET i guess, even am very new to this.

@thorburn1

This comment has been minimized.

Copy link
Author

thorburn1 commented Jan 6, 2017

debug message :
Waiting for connection
:ref 1
please start sntp first !
State: sending Client Hello (1)
:wr
:sent 73
:rn 1370
:ww
:rd 5, 1370, 0
:rdi 1370, 5
:rd 74, 1370, 5
:rdi 1365, 74
State: receiving Server Hello (2)
:rd 5, 1370, 79
:rdi 1291, 5
:rd 1286, 1370, 84
:rdi 12:rch 1370, 1370
:rch 2740, 1370
:rch 4110, 749
86, 1286
:c 1286, 1370, 4859
:rd 3480, 3489, 0
:rdi 1370, 1370
:c 1370, 1370, 3489
:rdi 1370, 1370
:c 1370, 1370, 2119
:rdi 749, 740
State: receiving Certificate (11)
:rd 5, 749, 740
:rdi 9, 5
:rd 4, 749, 745
:rdi 4, 4
:c0 4, 749
State: receiving Server Hello Done (14)
State: sending Client Key Exchange (16)
:wr
:sent 267
:ww
:wr
:sent 6
:ww
State: sending Finished (16)
:wr
:sent 69
:rn 75
:ww
:rd 5, 75, 0
:rdi 75, 5
:rd 1, 75, 5
:rdi 70, 1
:rd 5, 75, 6
:rdi 69, 5
:rd 64, 75, 11
:rdi 64, 64
:c0 64, 75
State: receiving Finished (16)
:ref 2
pos:0 len:65 invalid hex sequence: â€
:ur 2
:wr
:sent 53
:rn 53
:rcl
:abort
:ww
Alert: close notify
:ur 1
:del
http result:-1
pm open,type:2 0

@pieman64

This comment has been minimized.

Copy link

pieman64 commented Jan 6, 2017

@thorburn1 you are lucky that I have been doing some fingerprint stuff in the last few days. After a very minor mod to your sketch Serial Monitor shows:

[HTTP-Client][handleHeaderResponse] RX: 'HTTP/1.1 200 OK'
[HTTP-Client][handleHeaderResponse] RX: 'Date: Fri, 06 Jan 2017 10:52:26 GMT'
[HTTP-Client][handleHeaderResponse] RX: 'Server: Apache/2.2.31 (Amazon)'
[HTTP-Client][handleHeaderResponse] RX: 'X-Powered-By: PHP/5.5.30'
[HTTP-Client][handleHeaderResponse] RX: 'Content-Length: 106'
[HTTP-Client][handleHeaderResponse] RX: 'Connection: close'
[HTTP-Client][handleHeaderResponse] RX: 'Content-Type: text/html; charset=UTF-8'
[HTTP-Client][handleHeaderResponse] RX: ''
[HTTP-Client][handleHeaderResponse] code: 200
[HTTP-Client][handleHeaderResponse] size: 106
http result:200

OK so what's the mod. For some reason you have an invisible character at the start of your fingerprint and when I pulled down your fingerprint from https://www.grc.com/fingerprints.htm it seems to have given me the same. Simply go to the position in your sketch before the first F in the fingerprint and watch as you do a backspace on the keyboard. It will remove the invisible character and you will be good to go.

@thorburn1

This comment has been minimized.

Copy link
Author

thorburn1 commented Jan 6, 2017

@pieman64 That solved the problem I was fighting for since like 9 hours , thanks a lot.Never expected such error 💃

@thorburn1 thorburn1 closed this Jan 6, 2017

@thorburn1 thorburn1 reopened this Jan 6, 2017

@thorburn1 thorburn1 closed this Jan 6, 2017

@thorburn1

This comment has been minimized.

Copy link
Author

thorburn1 commented Jan 6, 2017

//INCASE SOMEONE IN FUTURE WANTS, THIS IS THE WORKING CODE TO SEND JSON DATA TO HTTPS API USING ESP8266 (SSL)

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>

void setup() {

Serial.begin(115200);
WiFi.begin("iot", "oksurewhynot");

while (WiFi.status() != WL_CONNECTED)

delay(500);
Serial.println("Waiting for connection");

}

void loop() {

if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status

HTTPClient http;
http.begin("https://fusmin.com/hooterstatus/api/HooterStatusUpdate.php" , "F6 XX XX XX XX XX XX XX XX XX XX BA D2 F5 30 AA FC B8 D3 7A‎");
http.addHeader("Content-Type", "application/json");
String postMessage = "{"chip_id":"rohitesp", "power":"2"}";
int httpCode = http.POST(postMessage);
Serial.print("http result:");
Serial.println(httpCode);
http.writeToStream(&Serial);
String payload = http.getString();
http.end();

}else{

Serial.print("Error in Wifi connection");

}

delay(30000); //Send a request every 30 seconds

}

@sergeykobzarenko

This comment has been minimized.

Copy link

sergeykobzarenko commented Dec 6, 2017

it doesnt help if you just changed http to https

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment