如何添加“ Authorization = Bearer”? Inphi中的Indy标头? [英] How to add a "Authorization=Bearer" header with Indy in Delphi?
问题描述
我正在尝试使用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
授权:Bearer eyJhbGciOiJSUzI1NiJ9 .....
内容类型:application / json
POST /somepath HTTP/1.1
Host: someurl.com.br
Authorization: Bearer eyJhbGciOiJSUzI1NiJ9.....
Content-Type: application/json
(省略了正文内容)
由Delphi生成的Indy请求,由HTTP分析器捕获(由于缺少 Authorization = Bearer部分,因此始终返回401 Forbidden错误):
POST / somepath HTTP / 1.1 < br>
主持人:someurl.com.br
内容类型:application / json
POST /somepath HTTP/1.1
Host: someurl.com.br
Content-Type: application/json
(省略正文)
我尝试使用下面的代码添加标头,但是标有 Authorization = Bearer eyxxxxxx ...的标头部分不是并非根据请求生成,返回401禁止错误。
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);
用':'代替'='之后,我收到了预期的响应,就像收到的一样通过邮递员。
After replacing the '=' by ':', I received the expected response, like the one received by POSTMAN.
这篇关于如何添加“ Authorization = Bearer”? Inphi中的Indy标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!