convert string post request to char array

I have made sketch with arduino uno and esp8266 in which i send measures of HC-SR04 to a website. I want to convert the code so I can use char arrays instead strings. How can I do this? Specifically how can i convert the string postRequest and the string data from string to char array?Can someone help me with a few lines of code? My code is:

#include <SoftwareSerial.h>
SoftwareSerial espSerial =  SoftwareSerial(2,3);  

//Wifi network SSID password
String ssid="ffffff";
String password ="*******";

boolean DEBUG=true;

int echoPin = 12; 
int trigPin = 13; 

long duration, distance; 
int alarm;

String alarms;

String data;
String server = "www.example.com"; // Send data to this site

String uri = "/add2.php";

int error = 0;
int cancel = 0;

void showResponse(int waitTime){
    long t=millis();
    char c;
    while (t+waitTime>millis()){
      if (espSerial.available()){
        c=espSerial.read();
        if (DEBUG) Serial.print(c);
      }
    }
                   
}

boolean websiteWrite(long value1, int value2){ 
  if (value2 == 0){
    alarms = "OFF";
  }else{
    alarms = "ON";
  }
  
  data = "distance=" + String(value1) + "&alarm=" + alarms;
  
  espSerial.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
  
  if( espSerial.find("OK")) {
  
  Serial.println("TCP connection ready"); cancel = 0;
  
  }
  else{
    cancel = cancel + 1;  Serial.println("cancel="); Serial.println(cancel);
  }
  delay(1000);
  
  String postRequest =
  
  "POST " + uri + " HTTP/1.0\r\n" +
  
  "Host: " + server + "\r\n" +
  
  "Accept: *" + "/" + "*\r\n" +
  
  "Content-Length: " + data.length() + "\r\n" +
  
  "Content-Type: application/x-www-form-urlencoded\r\n" +
  
  "\r\n" + data;
  
  String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
  
  espSerial.print(sendCmd);
  
  espSerial.println(postRequest.length() );
  
  delay(500);
  
  if(espSerial.find(">")) { Serial.println("Sending.."); espSerial.print(postRequest);
  
  if( espSerial.find("SEND OK")) { Serial.println("Packet sent");
  
  while (espSerial.available()) {
  
  String tmpResp = espSerial.readString();
  
  Serial.println(tmpResp);
  
  }
  
  // close the connection
  
  espSerial.println("AT+CIPCLOSE");
  
  }
    error = 0;
  }else{
    error = error + 1;
    Serial.println("error=");
    Serial.println(error);
    
  }
  
  if(cancel >=15 || error >= 15){software_Reset() ;}
  
}

void setup() {
  delay(150000); 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  DEBUG=true;           
  Serial.begin(9600); 
  
  espSerial.begin(9600);  
  
  //espSerial.println("AT+RST");         
  //showResponse(1000);
  
  //espSerial.println("AT+UART_DEF=9600,8,1,0,0");    
  //showResponse(1000);
  
  espSerial.println("AT+CWMODE=1");   
  showResponse(1000);

  espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");  
  showResponse(5000);

  if (DEBUG)  Serial.println("Setup completed");  
}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58.2;
  
  if (distance<=8){
    alarm=1;
  }else{
    alarm=0;
  }
  
      if (isnan(distance)) {
        if (DEBUG) Serial.println("Failed of calculation the distance");
      }
      else {
          if (DEBUG)  Serial.println("Distance="+String(distance)+" cm");
           websiteWrite(distance,alarm);                                      // Write values to website
      }
  
    
  //Needs 60 sec delay between updates    
  delay(60000);    
}

void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile ("  jmp 0");  
}

Specifically how can i convert the string postRequest and the string data from string to char array?

Converting postRequest to a string, instead of a String is easy.

   const int postRequestSize = ???; // You need to count how many characters in the largest request, and replace ??? with that value (or larger)
   char postRequest[postRequestSize+1];

   snprintf(postRequest,
      postRequestSize,
      "POST %s HTTP/1.0\r\nHost: %s\r\nAccept: */*\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n%s",
      uri.c_str(),
      server.c_str(),
      data.length(),
      data.c_str());

Of course, there is no reason for server to be a String, so you'll want to change server to a char *, and get rid of the .c_str() in the snprintf() call (for server).

Likewise, there is no reason for uri to be a String.

And, your goal is to make data not be a String, so you'll need to change data.length() to strlen(data), and dump the .c_str() after data in the snprintf() call.

I'm sure that you can see, now, how to make data not be a String.

It does not work again. What will the postRequest be? char postRequest[]=?

What will the car be? char postRequest[]=?

No.
Declare postRequest as an array of chars large enough to hold the request, then use snprinf() to populate it with text and data to turn it into a C style string as in Paul's example

It does not work again.

That is the most useless thing you say here. You have some code, where you made some changes. You failed to post the modified code. The code compiles, or it doesn't. You failed to say which. If it doesn't compile, there are error messages that you could have shared, but didn't. If it does compile, the code does something that you failed to describe.

I have modified my code according Paul's example as:

void(* software_Reset) (void) = 0;

#include <SoftwareSerial.h>
SoftwareSerial espSerial =  SoftwareSerial(2,3);  

boolean DEBUG=true;

int echoPin = 12; 
int trigPin = 13; 

long duration, distance; 
int alarm;

const int distancesSize = 20;
char distances[distancesSize+1];

char* alarms;

const int dataSize = 200; 
char data[dataSize+1];

const char* server = "www.example.com"; // Send data to this site

const char* uri = "/add5.php";


int cancel = 0;
int error = 0;

void showResponse(int waitTime){
    long t=millis();
    char c;
    while (t+waitTime>millis()){
      if (espSerial.available()){
        c=espSerial.read();
        if (DEBUG) Serial.print(c);
      }
    }                 
}

boolean websiteWrite(long value1, int value2){   
  if (value1 > 500){
   snprintf(distances,
      distancesSize,
      ("%s"),
      ">500");
  }else{
    snprintf(distances,
      distancesSize,
      ("%d"),
      value1);
  }
 if (value2 == 0){
    alarms = "OFF";
  }else{
    alarms = "ON";
  }
  
  snprintf(data,
      dataSize,
      ("distance=%s &alarm=%s"),
      distances,
      alarms);
  
  espSerial.println("AT+CIPSTART=\"TCP\",\"" "www.example.com" "\",80");//start a TCP connection.
  
  if( espSerial.find("OK")) {
  
  Serial.println("TCP connection ready"); error = 0;
  
  }
  else{
    error = error + 1;  Serial.println("error="); Serial.println(error);
  }
  delay(1000);
  
  const int postRequestSize = 500; 
  char postRequest[postRequestSize+1];

   snprintf(postRequest,
      postRequestSize,
      "POST %s HTTP/1.0\r\nHost: %s\r\nAccept: */*\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n%s",
      uri,
      server,
      strlen(data),
      data);
  
  char* sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
  
  espSerial.print(sendCmd);
  
  espSerial.println(strlen(postRequest));
  
  delay(500);
  
  if(espSerial.find(">")) { Serial.println("Sending.."); espSerial.print(postRequest);
      
  if( espSerial.find("SEND OK")) { Serial.println("Packet sent");
  
  while (espSerial.available()) {
  
  String tmpResp = espSerial.readString();
  
  Serial.println(tmpResp);
  
  }
  
  // close the connection
  
  espSerial.println("AT+CIPCLOSE");
  
  }
    cancel = 0;
  }else{
    cancel = cancel + 1;
    Serial.println("cancel=");
    Serial.println(cancel);
    
  }
  
  if(error >=15 || cancel >= 15){software_Reset() ;}
  
 
}


void setup() {
  delay(150000); 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  DEBUG=true;           
  Serial.begin(9600); 
  
  espSerial.begin(9600);  
  
  espSerial.println("AT+CWMODE=1");   
  showResponse(1000);

  espSerial.println("AT+CWJAP=\"ssid\",\"password\""); 
  
  showResponse(5000);
  
   if (DEBUG)  Serial.println("Setup completed");  
}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58.2;
  
  if (distance<=8){
    alarm=1;
  }else{
    alarm=0;
  }
      if (isnan(distance)) {
        if (DEBUG) Serial.println("Failed of calculation the distance");
      }
      else {
          if (DEBUG)  Serial.println("Distance="+String(distance)+" cm");
           websiteWrite(distance,alarm);                                      // Write values to website
      }
  
    
  //Needs 60 sec delay between updates    
  delay(60000);    
}

Did I properly convert?
It compile and run normally but sometimes crashes to packet send untli reset arduino. What's the reason for this?Is it due to poor internet connection the above problem?

How can i convert these lines of code from string to char array?
-espSerial.println("AT+CIPSTART="TCP","" + server + "",80");
-espSerial.println("AT+CWJAP=""+ssid+"",""+password+""");
-String tmpResp = espSerial.readString(); Serial.println(tmpResp);

What value will I initially put in postRequestSize and other sizes?How can I figure out this number?

   snprintf(distances,
      distancesSize,
      ("%s"),
      ">500");

This uselessly wastes resources.

  strncpy(distances, distancesSize, ">500");

Don't waste the resources using any variation of printf() when the only thing you are doing is assigning one string to another.

(format) (strings) (do) (NOT) (need) (to) (be) (in) (parentheses).

  char* sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

That comment is complete rubbish.

  const int postRequestSize = 500;
  char postRequest[postRequestSize+1];

Is this REALLY a reasonable array size? I seriously doubt it.

but sometimes crashes to packet send untli reset arduino.

Once more in English, please.

How can i convert these lines of code from string to char array?

A string IS a NULL-terminated char array. A String is an abomination. Which are you talking about?

If it is a String that you want to get rid of, figure out which arguments are strings and which are Strings. Eliminating the Strings is trivial.

strncpy() and strncat() are all that you need.

There is nothing you can do about readString() returning a String. So, do NOT use that method.