Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Jhony9625
#81413 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:

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.
User avatar
By Bonzo
#81420 This is some code I am using - if you are trying to contact an https site you will have problems. Out of interest if I used deep sleep but after a couple of connections they were "refused" for a couple of hours and I am not sure why. I assume it is something in my setup loop as if I use delay instead of deep sleep it is OK.

Technically this is also a GET as the data is in the URL.

I recommend getting the code to work in a short basic program first as it will be easier to find any problems. You can then implement it into your main program when you know it is working.

If you are interested you can see my data here - served up from a database with php and displayed with JavaScript: http://rubblewebs.co.uk/IOT/

Code: Select all#include <ESP8266HTTPClient.h> // head of program

const char *host = "http://URL to page";

// Below in the Loop
HTTPClient http;    //Declare object of class HTTPClient

  //Post Data
  String postData = "data=" + data ;

  http.begin(host);              //Specify request destination
  delay(1000); // See if this prevents the problm with connection refused and deep sleep
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type header

  int httpCode = http.POST(postData);   //Send the request
  String payload = http.getString();    //Get the response payload

  Serial.println(httpCode);   //Print HTTP return code
  Serial.println(payload);    //Print request response payload

  http.end();  //Close connection