HTTP/1.1 400 Bad Request

Asked

Viewed 760 times

-2

I have a method in DELPHI that performs a Post using TIDHTTP. The first time it works normally, but from the second it presents me the error: exception class EIdHTTPProtocolException with message 'HTTP/1.1 400 Bad Request the method is like this:

function TApiAPE.postJson(uri: string; sJsonToSend: string): TRetornoAPE;
  var JsonToSend: TStringStream;
      sResponse: string;
      jsonObj: TJSONObject;
begin
  result.ok := false;
  result.mensagem  := '';
  result.idRetorno := 0;

  JsonToSend := TStringStream.Create( Utf8Encode( sJsonToSend ) );
  try
    FHTTPClient.Request.Method := 'POST';
    try
      sResponse := FHTTPClient.Post(uri, JsonToSend);  //<<< AQUI ELE APRESENTA O ERRO NA SEGUNDA CHAMADA AO METODO
      if FHTTPClient.ResponseCode=200 then
      begin
        //{
        //  "MensagemRetorno": "Requisição feita com sucesso",
        //  "idGerado": 510387
        //}
        jsonObj := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(sResponse),0) as TJSONObject;
        if jsonObj.Get('idGerado')<>nil then
        begin
          result.ok := true;
          result.mensagem  := jsonObj.GetValue('MensagemRetorno').Value ;
          result.idRetorno := StrToIntDef( jsonObj.GetValue('idGerado').Value,0 );
        end;
      end;
    except
      on E: EIdHTTPProtocolException do
      begin
        result.codigoRetorno := FHTTPClient.ResponseCode;
        FHTTPClient.Disconnect;
      end;
    end;
  finally
    JsonToSend.free;
  end;
end;

Does anyone know what it can be? the contents of the variable sJsonToSend is the same as the previous

1 answer

-1


Friend, save your Json in Text and use the site https:/jsonlint.com/ to validate your Json file, then use Postman to send the request to your server, so you isolate, and know if it is the component, or indeed the Json being sent, usually the return 'HTTP/1.1 400 Bad Request', means that the sent Json is out of pattern, be an integer number, a date, a wrong string.

  • Thanks fellow, following your tip discovered by API requirement need to inform a differentiated ID to each request. Solved

Browser other questions tagged

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