HTTP POST on a web service (ESP32)

I am trying to send a string through HTTP POST from arduino to a simple web service that only receives a string and writes it to a .txt file.

I do not have any knowledge about HTTP, and I have tested several codes with things that I have been seeing on the Internet always without success.

My biggest problem is knowing how to configure and send an HTTP POST to the web service. I appreciate if somebody could help me with this part.

My current basic code is:

#include <WiFi.h>


const char* nome_rede = "MEO-*****";
const char* password =  "********";

const char* host = "188.251.100.123";  //ip adress of localhost pc
const int httpPort = 80;

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  conectar_rede();
}

void loop() {
  http();
}

void http() {

  String PostData = "recebida=1234567890"; //test string

  WiFiClient client = server.available();

  if (!client.connect(host, httpPort)) {  //conectar a um determinado host
    Serial.println("conecao ao host falhada");
    loop();
  }

  if (client) {

    if (client.connected()) {
      Serial.println("Connected to client");
      client.println("POST http://188.251.100.123:51481/WebService.asmx?op=HelloWorld HTTP/1.1");
      client.println("Host: 188.251.100.123");
      client.println("Cache-Control: no-cache");
      client.println("Content-Type: application/x-www-form-urlencoded");
      client.println("User-Agent: Arduino/1.0");
      client.println("Connection: close");
      client.print("Content-Length: ");
      client.println(PostData.length());
      client.println();
      client.print(PostData);

      delay(300);
      client.stop();
      Serial.println("Client stop");
    }


    else Serial.println("Cliente not conected");
    // close the connection:

  }
  else Serial.println("Cliente not found");

  delay(2000);
}

void conectar_rede() {
  WiFi.begin(nome_rede, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("A conectar á rede: ");
    Serial.println(nome_rede);
  }
  Serial.println("Conectada");
  Serial.print("IP address do ESP32: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

The board used is the esp32

HTTP POST supplied in the web service is:

POST /WebService.asmx/HelloWorld HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

recebida=string

Thank you all for your time spent trying to help me.

  WiFiClient client = server.available();

This would be the client that connected to your server. You aren't running a server, so this function will always return NULL.

I already changed and just put:

 WiFiClient client;

loop() calls http(), which calls loop(). That is a recipe for disaster.

http() should simply return if it can't proceed.

You need to run the client example AS IS, changing no more than the access credentials, if any need changing.

Then, you can modify it to do what you want.

There is so much wrong with your program that Ctrl-A, Ctrl-X and start over again is the only viable option.