How use POST with ESP8266HTTPClient
Posted: Thu Mar 28, 2019 9:44 am
Very good, initially excuse my English.
I am trying to communicate with a server by GET and POST. The GET I could already do it by receiving a Json file and taking the values I need from that file. Now I need to send these variables via POST:
grant_type: password
client_id: 2
client_secret: lIFOudKXgfP7hmzgpevs3lFNtQTJAgRvXC6OU0wQ
username: ADM123
password: aaaaaa
The code that I am implementing is the following:
Somebody could help me? Thank you.
I am trying to communicate with a server by GET and POST. The GET I could already do it by receiving a Json file and taking the values I need from that file. Now I need to send these variables via POST:
grant_type: password
client_id: 2
client_secret: lIFOudKXgfP7hmzgpevs3lFNtQTJAgRvXC6OU0wQ
username: ADM123
password: aaaaaa
The code that I am implementing is the following:
Code: Select all
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
// WiFi Parameters
const char* ssid = "BALTRAC"; //Nombre de la red Wi-Fi
const char* password = "baltrac1140824610"; //Contraseña de la red Wi-Fi.
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password); //Inicia la conexión con la red Wi-Fi.
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
}
void loop() {
// Check WiFi Status
if (WiFi.status() == WL_CONNECTED) { //Si hay acceso a internet.
HTTPClient http; //Objeto de clase HTTPClient
//http.begin("http://jsonplaceholder.typicode.com/todos/1"); //Conecta con servidor.
http.begin("http://18.223.239.136/api/v1/users/grant_client");
int httpCode = http.GET(); //Realiza una petición al servidor.
if (httpCode > 0) { //Si hay algo que recibir del servidor.
const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(8) + 370;
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.parseObject(http.getString());
// Parameters
int id = root["id"]; // 1
const char * name = root ["secret"]; // "Leanne Graham"
//const char * username = root ["username"]; // "Bret"
//const char * email = root ["email"]; // "Sincero@april.biz"
Serial.print("id: ");
Serial.println(id);
Serial.print("Secret: ");
Serial.println(name);
// Serial.print("Username:");
// Serial.println(username);
// Serial.print("Email:");
// Serial.println(email);
}
http.end(); //Close connection
/* grant_type:password
client_id:{{client_id}}
client_secret:{{client_secret}}
username:ADM123
password:aaaaaa */
//HTTPClient http;
String grant_type, client_id ,client_secret, username, password, message;
grant_type = "password";
client_id = "2";
client_secret = "lIFOudKXgfP7hmzgpevs3lFNtQTJAgRvXC6OU0wQ";
username = "ADM123";
password = "aaaaaa";
message = "grant_type:" + grant_type + "&" + "client_id:" + client_id + "&" + "client_secret:" + client_secret + "&" + "username:" + username + "&" + "password:" + password;
Serial.println(message);
http.begin("http://18.223.239.136/oauth/token");
http.addHeader("Content-Type", "application / x-www-form-urlencoded"); //http.addHeader("Content-Type", "text/plain");
httpCode = http.POST(message);
if(httpCode > 0) {
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
}
else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
// Delay
delay(10000);
}
Somebody could help me? Thank you.