ESP8266HTTP GET/PUT Authentication
Posted: Fri Apr 10, 2020 1:46 pm
Hello everyone!
I've been trying to access an API with my ESP8266 but with no luck. The API returns data in JSON format and i parse it using the ArduinoJson library. All of the has worked well with HTTP based addresses such as http://jsonplaceholder.typicode.com/users/2 but as soon as i try to use a HTTPS based address the function returns a "MAX INT". All of this is new to me and i would appreciate some help with it.
Im able to retrive data using cURL to access the API. Below is the command
curl -u xxxx:api_token -X GET https://xxx.com/api
Below are the API instructions that.
API Instructions:
HTTP POST or HTTP Basic Auth needs to be used to get the data.
Im trying to use HTTP Basic Auth.
"For HTTP Basic Auth you have to add the Authorization header with the request. The Authorization header is constructed as follows:"
Username and password are combined into a string username:password or if you use the api token it should be combined xxxx:api_token (xxx indicating user's personal token)
The resulting string literal is then encoded using Base64
The authorization method and a space i.e. "Basic " is then put before the encoded string.
For example, if the user agent uses 'Aladdin' as the username and 'open sesame' as the password then the header is formed as follows: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Below is the part of the code that im trying to use.
I have tried to not Base64 encode the token and get the same problem. Ive also tried to use the ".setAuthorization(Basic MyTokenInBase64);" but it doesn't work.
Ive also tried usign the HTTPS example to do the GET request but it returns the same thing.
The above code combined with the first code returns all of the HTML code on the web page but im unable to get any JSON data from similar test websites nor the actual website im trying to get the data from.
I have no idea whats going. It would be much appreciated if anybody could shine a light on this.
I've been trying to access an API with my ESP8266 but with no luck. The API returns data in JSON format and i parse it using the ArduinoJson library. All of the has worked well with HTTP based addresses such as http://jsonplaceholder.typicode.com/users/2 but as soon as i try to use a HTTPS based address the function returns a "MAX INT". All of this is new to me and i would appreciate some help with it.
Im able to retrive data using cURL to access the API. Below is the command
curl -u xxxx:api_token -X GET https://xxx.com/api
Below are the API instructions that.
API Instructions:
HTTP POST or HTTP Basic Auth needs to be used to get the data.
Im trying to use HTTP Basic Auth.
"For HTTP Basic Auth you have to add the Authorization header with the request. The Authorization header is constructed as follows:"
Username and password are combined into a string username:password or if you use the api token it should be combined xxxx:api_token (xxx indicating user's personal token)
The resulting string literal is then encoded using Base64
The authorization method and a space i.e. "Basic " is then put before the encoded string.
For example, if the user agent uses 'Aladdin' as the username and 'open sesame' as the password then the header is formed as follows: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Below is the part of the code that im trying to use.
Code: Select all
//Used packages
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
uint16_t httpCode{};
HTTPClient http;
WiFi.begin(user, pass);
http.begin("URL");
http.addHeader("Authorization:", "Basic MyTokenInBase64", true);
httpCode = http.GET();
//Further down in code
deserializeJson(doc, http.getString(), DeserializationOption::Filter(filter));
I have tried to not Base64 encode the token and get the same problem. Ive also tried to use the ".setAuthorization(Basic MyTokenInBase64);" but it doesn't work.
Ive also tried usign the HTTPS example to do the GET request but it returns the same thing.
Code: Select all
#include <WiFiClientSecureBearSSL.h>
const uint8_t fingerprint[20] = {0x40, 0xaf, 0x00, 0x6b, 0xec, 0x90, 0x22, 0x41, 0x8e, 0xa3, 0xad, 0xfa, 0x1a, 0xe8, 0x25, 0x41, 0x1d, 0x1a, 0x54, 0xb3};
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setFingerprint(fingerprint);
http.begin(client, "https://jigsaw.w3.org/HTTP/connection.html");
The above code combined with the first code returns all of the HTML code on the web page but im unable to get any JSON data from similar test websites nor the actual website im trying to get the data from.
I have no idea whats going. It would be much appreciated if anybody could shine a light on this.