【问题标题】:how do I transform the CURL in Rest command Delphi如何在 Rest 命令 Delphi 中转换 CURL
【发布时间】:2021-05-03 07:18:19
【问题描述】:

如何在 Rest 命令中转换 CURL?

怎么了

curl -X GET --header "Accept: application/json" --header "X-Authorization: Bearer "Token"" "http://console.agrolog.io:8080/api/auth/user"

德尔福代码:

  RESTClient1.BaseURL := 'http://console.agrolog.io:8080/api/';

  RESTRequest1.Method := TRESTRequestMethod.rmGET;
  RESTRequest1.Resource := 'auth/user';

  RESTRequest1.Params.Clear;
  with RESTRequest1.Params.AddItem do
    begin
        ContentType := ctAPPLICATION_JSON;
        name        := 'Authorization';
        Value       := 'X-Authorization: Bearer ' + '"'+Strtoken+'"' ;
        Kind        :=  pkHTTPHEADER;
    end;

  RESTRequest1.Execute;

错误是EHTTPProtocolException,消息为'HTTP/1.1 401'

【问题讨论】:

  • 你的代码有什么问题?请提供您从 API 或您的程序收到的错误,并添加更具体的问题。
  • 不确定您遇到了哪个错误。我只能猜测。但是,如果您的身份验证不起作用,请仔细检查您是否真的需要在不记名令牌周围加上引号。我平时遇到的格式是Bearer xxxxxxxxxxxxxxxxxx(不带引号)。
  • 错误是 EHTTPProtocolException with Message 'HTTP/1.1 401'

标签: rest delphi


【解决方案1】:

你设置的 REST 参数都错了。

参数的Name 应该是'X-Authorization' 而不是'Authorization'(为什么?'Authorization' 是标准化的标头),您需要从其Value 中删除X-Authorization: 前缀。你也不应该设置它的ContentType

另外,您需要将RESTRequest1.Accept 设置为'application/json'

试试这个:

RESTClient1.BaseURL := 'http://console.agrolog.io:8080/api/';

RESTRequest1.Method := TRESTRequestMethod.rmGET;
RESTRequest1.Resource := 'auth/user';
RESTRequest1.Accept := 'application/json';

RESTRequest1.Params.Clear;
with RESTRequest1.Params.AddItem do
begin
  Name := 'X-Authorization'; // 'Authorization'
  Value := 'Bearer "'+Strtoken+'"';
  Kind := pkHTTPHEADER;
end;

RESTRequest1.Execute;

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 2021-10-08
    • 1970-01-01
    • 2016-10-24
    • 2020-10-21
    • 2015-08-23
    • 1970-01-01
    • 2012-05-10
    • 2019-06-04
    相关资源
    最近更新 更多