Search code examples
httpdelphipostrequestindy

TIdHttp not work method post empty body - Delphi


I have a problem when trying to make a request using the POST method when there is no information in the body, how do I use POST without having Parameters/Body ?

I tried to pass empty, but the code below returns the following error:

HTTP/1.1 500

try
  responseres := tstringlist.Create;
  retorno := TstringStream.Create;
  params := tstringlist.Create;
  //--------------------------------
  IdHttp1.Request.CustomHeaders.Values['Authorization']     := 'Bearer ' + txtAccessToken.Text;
  IdHttp1.Request.CustomHeaders.Values['x-integration-key'] := 'GYBtKWR5QjSwVxXXXxxXxXeUeOsUe0nOuc8HyTnyT1s';
  //--------------------------------
  IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
  IdHTTP1.Request.Charset     := 'utf-8';
  //--------------------------------
  IdHTTP1.Post('https://api.onvio.com.br/dominio/integration/v1/activation/enable', params, retorno);
  JsonResponse        := TJSONObject.ParseJsonValue(UTF8Decode(retorno.DataString)) as TJSONObject;
  IntegrationKey.Text := JsonResponse.GetValue<string>('integrationKey');

  MemoJson.Lines.Text := responseres.Text;
finally
  params.Free;
  responseres.Free;
end;

In postman, this works:

postman_img


Solution

  • I managed to solve it thanks to your help @Remy Lebeau, @Robert, @Dave Nottage. Thank you so much !!

    SOLUTION:

     retorno     := TstringStream.Create;
     params      := nil;
     responseres := TStringlist.Create;
    
     IdHttp1.Request.CustomHeaders.Clear;
     IdHttp1.Request.CustomHeaders.Values['Authorization']     := 'Bearer ' + txtAccessToken.Text;
     IdHttp1.Request.CustomHeaders.Values['x-integration-key'] := 'GYBtKWR5QjSwVxXXXxxXxXeUeOsUe0nOuc8HyTnyT1s';
     IdHttp1.Request.CustomHeaders.Values['Content-Length']    := '0';
     IdHttp1.Request.CustomHeaders.Values['Accept']            := '*/*';
     IdHttp1.Request.CustomHeaders.Values['Accept-Encoding']   := 'gzip, deflate, br';
     IdHttp1.Request.CustomHeaders.Values['Connection']        := 'keep-alive';
    
     IdHTTP1.Request.ContentType       := 'application/json';
     IdHTTP1.Request.Charset           := 'utf-8';
    
     IdHTTP1.Post('https://api.onvio.com.br/dominio/integration/v1/activation/enable', params, retorno);
     JsonResponse        := TJSONObject.ParseJsonValue(UTF8Decode(retorno.DataString)) as TJSONObject;
     IntegrationKey.Text := JsonResponse.GetValue<string>('integrationKey');
    
     MemoJson.Lines.Text := FormatJSON(retorno.DataString);
     JsonToDataset(FDMemTable1, '['+retorno.DataString+']');