问题描述
我正在尝试使用 access_token 执行 POST 请求,并且使用 POSTMAN 可以正常工作,但是当我尝试在 Delphi 上执行相同的请求时,我找不到添加Authorization=Bearer eyxxxxxx"的方法..."到请求标头,就像 POSTMAN 一样.
I'm trying to do a POST request using an access_token, and it works fine using POSTMAN, but when I try to do the same request on Delp I can't find a way to add the "Authorization=Bearer eyxxxxxx..." to the Request header, as POSTMAN does.
POSTMAN 请求(运行良好):
POST/somepath HTTP/1.1
主机:someurl.com.br
授权:承载 eyJhbGciOiJSUzI1NiJ9.....
内容类型:application/json
POST /somepath HTTP/1.1
Host: someurl.com.br
Authorization: Bearer eyJhbGciOiJSUzI1NiJ9.....
Content-Type: application/json
(省略正文内容)
Delphi 生成的 Indy 请求,被 HTTP Analyzer 捕获(总是返回 401 Forbidden 错误,因为缺少Authorization=Bearer"部分):
POST/somepath HTTP/1.1
主机:someurl.com.br
内容类型:application/json
POST /somepath HTTP/1.1
Host: someurl.com.br
Content-Type: application/json
(省略正文内容)
我尝试使用下面的代码添加标头,但在请求时未生成带有Authorization=Bearer eyxxxxxx..."的标头部分,返回 401 Forbidden 错误.
I've tried to add the header using the code below, but the header part with the "Authorization=Bearer eyxxxxxx..." isn't generated on Request, returning the 401 Forbidden error.
FIdHTTP.Request.CustomHeaders.FoldLines := False;
FIdHTTP.Request.CustomHeaders.Add('Authorization=Bearer ' + txtToken.Text);
推荐答案
刚发现问题.我在授权"和承载"字样之间添加了错误的分隔符.
Just found the problem. I added the wrong separator between the "Authorization" and "Bearer" words.
错误:
FIdHTTP.Request.CustomHeaders.FoldLines := False;
FIdHTTP.Request.CustomHeaders.Add('Authorization=Bearer ' + txtToken.Text);
正确:
FIdHTTP.Request.CustomHeaders.FoldLines := False;
FIdHTTP.Request.CustomHeaders.Add('Authorization:Bearer ' + txtToken.Text);
将 '=' 替换为 ':' 后,我收到了预期的响应,就像 POSTMAN 收到的响应一样.
After replacing the '=' by ':', I received the expected response, like the one received by POSTMAN.
这篇关于如何添加“授权=持有人"在 Delphi 中使用 Indy 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!