Consume Rest API in Delphi

Asked

Viewed 1,434 times

0

I am trying to consume API Rest, made in php, from a Delphi application. In summary the API receives a field called name, another field is the parts parts, but in this case the field is a parts Arry with the following structure: parts[]['email'], parts[]['function'], in sequence a rejectable field of type Boolean and finally the file that will always be a pdf. Follows the code:

var
  params: TIdMultipartFormDataStream;
  ss: TStringStream;
   arq : TMemoryStream;
   IdHTTP : TIdHTTP;
   email, funcao : array of String;
begin

  params := TIdMultiPartFormDataStream.Create;
  email := ['[email protected]'];
  funcao := ['assinar'];
  ss := TStringStream.Create('');
  params.AddFormField('nome', 'Contrato aluno');
  params.AddFormField('partes[]', email[0]);
  params.AddFormField('parte[]', funcao[0]);
  params.AddFormField('rejeitavel', 'false');
  params.AddFile('Contrato','C:\arquivos\Contrato.pdf', 'application/pdf');
 // params.AddFormField('nome', 'Pasta Teste');

  Try
    IdHTTP := TIdHTTP.Create();
    IdHTTP.Request.CustomHeaders.Clear;
    IdHTTP.Request.Clear;
    IdHTTP.Request.ContentType := 'multipart/form-data; boundary=----BOUNDARY';
    IdHTTP.Request.CharSet := 'utf-8';
    IdHTTP.Request.CustomHeaders.AddValue('X-Autntiq-Api', 'xxxxxxxxxxx');
  finally
    IdHTTP.Post('https://api.xxxx.com.br/documentos.json', params,ss);      
  end;

end;

when executed it returns the error:

Project xxxxxx Raised Exception class Eidhttpprotocolexception with message 'HTTP/1.1 422 Unprocessable Entity'.

The HTTP 422 Unprocessable Entity response code indicates that the server understands the type of content of the request entity, and the request syntax is correct, but it was not possible to process the present instructions.

I tested with another API method and was successful. I can’t identify if the problem is in Camo parts[] as array or uploading file.

I thank you for your help and glorify God for the life of all!!!!

  • (HTTP) 422 Unprocessable Entity means that the server understood the content and syntax of the request, but was unable to process the instructions. Summary: it seems that the error is in the PHP code.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.