• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

HTTP POST issue with ESP32

Q&A Forum › Category: ESP32 › HTTP POST issue with ESP32
0 Vote Up Vote Down
Eric Herrmann asked 2 years ago

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!

Question Tags: ESP32 HTTP POST HEADER BODY JSON
3 Answers
0 Vote Up Vote Down
Eric Herrmann answered 2 years ago

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.
 

0 Vote Up Vote Down
Eric Herrmann answered 2 years ago

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 🙂
 
 

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi Eric.
I’m glad you found the solution to your problem.
I’ll close this issue.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.