String compare ... clarification

int  postResult;
…
…
  int httpCode = http.POST(jsonDataChar);   //Send the data to save on server
  
  String payload = http.getString();             //Get the response payload
  String postSuccess = "200" ; 
  Serial.println(httpCode);                         //Print HTTP return code
  postResult = payload.compareTo(postSuccess); 
  if (postResult == 0 ) {
    digitalWrite(postLed, HIGH);
  }
  else{
     digitalWrite(postLed, LOW);
  }

In this code snippet where I am checking if a POST method was successful I am getting a "200" response from the server. But the postLed always remains OFF. ( The LED is fine otherwise )

What is the mistake in the code ?

What do you see if you print payload ?

UKHeliBob:
What do you see if you print payload ?

400 when I POST an empty Json string.
200 when I POST a valid json string.

POST result.JPG

POST result.JPG

Is there by any chance a space before or after the number ?
Try printing ">" immediately before it and "<" immediately after it

UKHeliBob:
Is there by any chance a space before or after the number ?
Try printing ">" immediately before it and "<" immediately after it

Yes checked. There are no spaces.

POST result.JPG

POST result.JPG

How about

 postResult = payload.indexOf(postSuccess); 
 if (postResult != -1) {
   digitalWrite(postLed, HIGH);
 }
 else{
    digitalWrite(postLed, LOW);
 }

lesept:
How about

 postResult = payload.indexOf(postSuccess); 

if (postResult != -1) {
  digitalWrite(postLed, HIGH);
}
else{
    digitalWrite(postLed, LOW);
}

With this I expected the LED to be always on and so it is when checked.

Its a bit of mystery that this simple code does not work …

Incidentally is this the general method to check if an HTPP POST method was successful and light up an LED ?

Its a bit of mystery that this simple code does not work …

Try printing the value of postResult before to you test it. Is its value what you expect ?

In your images above when we see 200 or 400 is it the result of printing httpcode or payload?

Can you post the content of the serial monitor when you print everything : httpcode,payload, postresult?

UKHeliBob:
Try printing the value of postResult before to you test it. Is its value what you expect ?

Yes already done … please refer post #4