idHTTP HTTP/1.1 401 Unauthorized error

Asked

Viewed 2,028 times

2

Good morning, Already test several tips and solutions on this subject here in the forum and outside too, I’m 5 days trying to perform a post using idHttp with Design Berlin.

This is my code;

procedure TfrmPlaPSync.Button8Click(Sender: TObject);
var
  HTTP: TIdHTTP;
  RequestBody: TStream;
  ResponseBody: string;
begin
  HTTP := TIdHTTP.Create;
  try
    try
      RequestBody := TStringStream.Create('{"cod_regiao": "41", "nome": "NORTE"}',TEncoding.UTF8);
      try
        HTTP.Request.Accept := 'application/json';
        HTTP.Request.ContentType := 'application/json';
        HTTP.Request.BasicAuthentication := true;
        HTTP.Request.Username := 'robinho';
        HTTP.Request.Password := 'rb823321';
        HTTP.Request.Connection  := 'keep-alive';
        HTTP.Request.UserAgent   := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36';
        HTTP.Request.AcceptEncoding := 'gzip, deflate';
        HTTP.Request.AcceptLanguage := 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4';
        HTTP.Request.CacheControl := 'no-cache';

        ResponseBody := HTTP.Post('http://localhost:888/api/millenium/regioes/inclui',
          RequestBody);
        Memo2.Lines.Add(ResponseBody);
        Memo2.Lines.Add(HTTP.ResponseText);
      finally
        RequestBody.Free;
      end;
    except
      on E: EIdHTTPProtocolException do
      begin
        Memo2.Lines.Add(E.Message);
        Memo2.Lines.Add(E.ErrorMessage);
      end;
      on E: Exception do
      begin
        Memo2.Lines.Add(E.Message);
      end;
    end;
  finally
    HTTP.Free;
  end;
  ReportMemoryLeaksOnShutdown := True;
end;

the API requests a basic authentication I’m passing in the header, but I’m getting the following error:

HTTP/1.1 401 Unauthorized
  • 1

    For some reason the server is rejecting your credentials, or the user is authenticated but does not have permission for the page to which you post. I suggest using a program like the Fiddler or the Wireshark to see how the Delphi application is mounting the application header.

  • P.s.: This is not a forum.

  • Good afternoon, I discovered that the problem was in the API that did not accept authentication. P.s: I’m sorry, but I thought it was forum, because I see several questions even from that error.

  • It’s not a forum, it’s a question and answer site. There’s a big difference. See the tour: https://answall.com/tour and then a general look at the Help Center.

  • I apologize again, just as I have several who are wrong.

1 answer

1

Friend, I believe that what is missing is the following:

IdHTTP.Request.Clear;
IdHTTP.Request.CustomHeaders.Clear;
IdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Basic ' + seu_token_autorizacao);

See only the header name and adapt to your need. It worked for me.

Hug!

Browser other questions tagged

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