带curl的Delphi Indy http.get返回:未经授权

发布于 2025-01-14 15:41:07 字数 1256 浏览 0 评论 0原文

根据供应商数据,我应该有:

  • Http 类型 GET
  • 响应类型:application/json
  • 参数:授权:bearer + Token
  • Curl:curl -X GET --header '接受:application/json' --header '授权:bearer + Token , 'http://localhost:8080/api/v1/doors'
  • 请求 URL: 'http://localhost:8080/api/v1/doors'

我已将其翻译为Delphi(Indy TidHttp):

procedure TForm42.Button2Click(Sender: TObject);
var
  resp: TMemoryStream;
begin
  resp := TMemoryStream.Create;
  try
    IdHTTP1.Request.Clear;
    IdHTTP1.Request.Accept := 'application/json';
    IdHTTP1.Request.BasicAuthentication := True;
    IdHTTP1.Request.CustomHeaders.FoldLines := False;
    IdHTTP1.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + TokenStr;
    IdHTTP1.Get('http://10.10.1.62:8080/api/v1/doors', resp);
    resp.Position := 0;
    memCall.Lines.LoadFromStream(resp);
  finally
    resp.Free;
  end;
end;

我在这里读了很多相关内容,所以最后我还添加了“折叠线”(向 TIdHttp 请求添加自定义标头,标头值有逗号) 我还尝试了“X-Authorization”作为参数,这是我从 R. Lebeau 那里读到的,但我得到的唯一反应是一个错误对话框,上面写着“401 未经授权”。

我对令牌字符串(->'bearer' + TokenStr)很确定,因为我在将字符串放入供应商试用时得到了答案。

有人知道我做错了什么吗?

According to the suppliers data i should have:

  • Http type GET
  • Response type: application/json
  • Parameter: Authorization: bearer + Token
  • Curl: curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer + Token, 'http://localhost:8080/api/v1/doors'
  • Request URL: 'http://localhost:8080/api/v1/doors'

I have translated this to Delphi(Indy TidHttp):

procedure TForm42.Button2Click(Sender: TObject);
var
  resp: TMemoryStream;
begin
  resp := TMemoryStream.Create;
  try
    IdHTTP1.Request.Clear;
    IdHTTP1.Request.Accept := 'application/json';
    IdHTTP1.Request.BasicAuthentication := True;
    IdHTTP1.Request.CustomHeaders.FoldLines := False;
    IdHTTP1.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + TokenStr;
    IdHTTP1.Get('http://10.10.1.62:8080/api/v1/doors', resp);
    resp.Position := 0;
    memCall.Lines.LoadFromStream(resp);
  finally
    resp.Free;
  end;
end;

I read a lot about it here, so finally i also added 'foldlines (Adding custom header to TIdHttp request, header value has commas)
I also tried 'X-Authorization' as parameter, something i read from R. Lebeau, but the only reaction i get is an errordialog saying '401 unauthorized'.

I'm sure about the Token string (-> 'bearer ' + TokenStr) because i get an answer when putting the string in the suppliers trial.

Does someone have an idea what i'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2025-01-21 15:41:07

使用自定义身份验证时,Request.BasicAuthentication 应为 False 而不是 True

并且您不需要设置 CustomHeaders.FoldLines 因为 TIdHTTP 已经默认禁用折叠(当时默认情况下并未禁用 另一个问题已发布)。

否则,代码的其余部分看起来不错。

不过,我建议在调用 LoadFromStream() 时指定 TEncoding.UTF8),例如:

procedure TForm42.Button2Click(Sender: TObject);
var
  resp: TMemoryStream;
begin
  resp := TMemoryStream.Create;
  try
    IdHTTP1.Request.Clear;
    IdHTTP1.Request.Accept := 'application/json';
    IdHTTP1.Request.BasicAuthentication := False;
    IdHTTP1.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + TokenStr;
    IdHTTP1.Get('http://10.10.1.62:8080/api/v1/doors', resp);
    resp.Position := 0;
    memCall.Lines.LoadFromStream(resp, TEncoding.UTF8);
  finally
    resp.Free;
  end;
end;

或者,使用 TIdHTTP.Get() 的重载code> 返回一个字符串

procedure TForm42.Button2Click(Sender: TObject);
begin
  IdHTTP1.Request.Clear;
  IdHTTP1.Request.Accept := 'application/json';
  IdHTTP1.Request.BasicAuthentication := False;
  IdHTTP1.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + TokenStr;
  memCall.Text := IdHTTP1.Get('http://10.10.1.62:8080/api/v1/doors');
end;

Request.BasicAuthentication should be False not True when using custom authentications.

And you don't need to set CustomHeaders.FoldLines as TIdHTTP already disables folding by default (it wasn't disabled by default at the time the other question was posted).

Otherwise, the rest of the code looks fine.

Though, I would suggest specifying TEncoding.UTF8 on the call to LoadFromStream()), eg:

procedure TForm42.Button2Click(Sender: TObject);
var
  resp: TMemoryStream;
begin
  resp := TMemoryStream.Create;
  try
    IdHTTP1.Request.Clear;
    IdHTTP1.Request.Accept := 'application/json';
    IdHTTP1.Request.BasicAuthentication := False;
    IdHTTP1.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + TokenStr;
    IdHTTP1.Get('http://10.10.1.62:8080/api/v1/doors', resp);
    resp.Position := 0;
    memCall.Lines.LoadFromStream(resp, TEncoding.UTF8);
  finally
    resp.Free;
  end;
end;

Or, using the overload of TIdHTTP.Get() that returns a string:

procedure TForm42.Button2Click(Sender: TObject);
begin
  IdHTTP1.Request.Clear;
  IdHTTP1.Request.Accept := 'application/json';
  IdHTTP1.Request.BasicAuthentication := False;
  IdHTTP1.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + TokenStr;
  memCall.Text := IdHTTP1.Get('http://10.10.1.62:8080/api/v1/doors');
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文