Hi all,
I am currently following the ESP32 course for a personal project.
I need to send an HTTP Post request to retrieve an API token.
I have built the request in Postman and it is working fine
The request is as follow:
POST /app/login HTTP/1.1
Host: euapi.gizwits.com
X-Gizwits-Application-Id: c70a66ff039d41b4a220e198b0fcc8b3
Content-Type: text/plain
Content-Length: 71
{
“username”: “my email address”,
“password”: “my password”
}
this is the http code result from postman (code view in postman)
The request is built with –>
Headers
{
X-Gizwits-Application-Id = c70a66ff039d41b4a220e198b0fcc8b3
}
Body
{
“username”: “my email address”,
“password”: “my password”
}
The request is properly working in Postman and returning this:
{
“token”: “my token”,
“uid”: “my uid”,
“expire_at”: expiration_date
}
Now in my code (VSCode + PlatformIO) I have created the following function and I am calling it in setup() –>
char GetToken() {
String httpRequestData;
String mylogin = MY_LOGIN;
String mypwd = MY_PWD;
HTTPClient http;
const char* serverName = “http://euapi.gizwits.com/app/login“;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
http.addHeader(“X-Gizwits-Application-Id”, “c70a66ff039d41b4a220e198b0fcc8b3”);
http.addHeader(“Content-Type”, “text/plain”);
// Data to send with HTTP POST
httpRequestData=”username=” + mylogin + “&password=” + mypwd;
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
Serial.print(“HTTP Response code: “);
Serial.println(httpResponseCode);
// Free resources
http.end();
}
I have not yet reached the step where I extract the JSON information with the token because I get an HTTP400 response which I guess means something is not well encoded in my request.
Can you please help me out?
Thanks!
Hi again,
Ok I am progressing (I hope!)
This seems to be a formatting issue with the body content.
I have tried different things and now it works (I mean I have an HTTP 200 answer) without changing the headers lines but by changing the body with:
String httpRequestData = String(“{\n\”username\”: \””) + mylogin + “\”,\n\”password\”: \”” + mypwd + “\”\n}”;
I’ll now start understanding how to decode the JSON answer and extract my token.
I could finally process the JSON response using the code from the ESP32 course (Module 13 Unit 7) with the methods JSON.parse() and JSON.typeoff() methods. This was very straightforward.
This course is really a must have!
Problem solved…until the next one 🙂