Error when trying to make a POST request

I've tried with HttpClient and I couldn't either, now I'm trying with <WiFiClientSecure.h>

But it is returning Bad Request 400

Does anyone know how I could try to make this POST request successfully?

I want to send the photo.png to the API

const char* host = "api.ocr.space";

void conectasecure()   {
  
  WiFiClientSecure client;
  client.setInsecure();
 
  if (client.connect(host, 443))  {

  url = "http://api.ocr.space/parse/image";
  photo = "http://i.imgur.com/fwxooMv.png";

   client.println("POST" + url + "HTTP/1.1");
   client.println("Host:" + String(host));
   client.println("apikey:K873541123456789");
   client.println("Content-Type:multipart/form-data");
   client.print(photo);
   
    long int tempo_inicio = millis();
     
    while (!client.available()) {
      Serial.print(".");
      delay(100);
      if ((tempo_inicio + tempo_espera) < millis()) {
        Serial.println();
        Serial.println("Sem Resposta.");
        break;
      }
    }
     
    Serial.println();  
     
    while (client.available()) {
      Serial.print(char(client.read()));
    }  
  } else {        
    Serial.println("A conexão ao " + String(host) + " FALHOU ");
  }
    
  client.stop();
  
}

When you looked at the log files of the web server, what is the error message being reported when your project makes a request?

This:

HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 16 Feb 2023 13:40:06 GMT
Connection: close
Content-Length: 326

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Verb</h2>
<hr><p>HTTP Error 400. The request verb is invalid.</p>
</BODY></HTML>

Your POST request is not a valid request

I put this empty line at the end of the request and it no longer returns an error, but it does not return anything, not even code 200

client.println("");

I tried again with HttpClient.h because it is simpler for my little knowledge. But it has always insisted on not recognizing the png photo format. It gives error 99.

I tried to create and add another filetype header, but not so

If it's via GET it works like a charm.


String url = "http://api.ocr.space/parse/image";

void conecta()   {

  HTTPClient http;
            
  http.begin(url);   
  http.addHeader("apikey", "K87354123456789");
  http.addHeader("Content-Type", "Content-Type: image/png"); 
  http.addHeader("File-Type", "filetype: png");
  String httpRequestData = "http://i.imgur.com/fwxooMv.png";               

  int httpResponseCode = http.POST(httpRequestData);


 
    if(httpResponseCode > 0){
    String response = http.getString();
    Serial.println(httpResponseCode);
    Serial.println(response);
  } else {
    Serial.print("Error on sending POST: ");
    Serial.println(httpResponseCode);
    http.end();
    }

    delay(5000);
   
}

ERROR

200
{"OCRExitCode":99,"IsErroredOnProcessing":true,"ErrorMessage":["Unable to recognize the file type","Unable to detect the file extension, or the file extension is incorrect, and no 'file type' provided in request. Please provide a file with a proper content type or extension, or provide a file type in the request to manually set the file extension."],"ProcessingTimeInMilliseconds":"0"}

Using this works. But how to transform it into a request in HttpClient.h ?

Dissatisfied, I changed the API, because I'm just testing out of curiosity, it's not a job. I am learning.

But it's amazing how even in this other API the image that I try to send by URL is also not recognized

void posthttp()   {

   url = "https://api.z-api.io/instances/3B90F2295A2E30DSHGDS7848J5F/token/0966E05GKJGJKGHHTR45/send-image";
    
   phone = "552294656878";    
   token = "0966E5897554785rkrD26523C";
   image = "https://i.imgur.com/fwxooMv.png";

  if(autorizaenvio == 0)   {
   if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
    
    HTTPClient http;   

    http.begin(url);
    http.addHeader("phone", phone);
    http.addHeader("image", image);
    http.addHeader("Content-Type", "image/png");
   
    int httpResponseCode = http.POST(url);
  
   if(httpResponseCode>0){
  
    String response = http.getString();                     
  
    Serial.println(httpResponseCode);
    Serial.println(response);         
   } else{
    Serial.print("Error on sending POST: ");
    Serial.println(httpResponseCode);
   }
  
   http.end();

 } else{
    Serial.println("Error in WiFi connection");   
   }
 
  } autorizaenvio = 1;
  
  delay(1000);
}

Does anyone have any ideas ?

Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.