带curl的Delphi Indy http.get返回:未经授权
根据供应商数据,我应该有:
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用自定义身份验证时,
Request.BasicAuthentication
应为False
而不是True
。并且您不需要设置
CustomHeaders.FoldLines
因为TIdHTTP
已经默认禁用折叠(当时默认情况下并未禁用 另一个问题已发布)。否则,代码的其余部分看起来不错。
不过,我建议在调用
LoadFromStream()
时指定TEncoding.UTF8
),例如:或者,使用
TIdHTTP.Get()
的重载code> 返回一个字符串
:Request.BasicAuthentication
should beFalse
notTrue
when using custom authentications.And you don't need to set
CustomHeaders.FoldLines
asTIdHTTP
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 toLoadFromStream()
), eg:Or, using the overload of
TIdHTTP.Get()
that returns astring
: