Convert CURL POST without body to RESTRequest Delphi Code / Write Delphi Code for Dropbox API /get_current_account endpoint

huangapple go评论80阅读模式
英文:

Convert CURL POST without body to RESTRequest Delphi Code / Write Delphi Code for Dropbox API /get_current_account endpoint

问题

请帮助我弄清楚如何正确编写Delphi代码,以匹配运行Curl POST命令而不指定请求体的结果。

我能够成功运行CURL命令,并且它返回了预期的帐户信息:

curl -X POST https://api.dropboxapi.com/2/users/get_current_account\
 --header "Authorization: Bearer <获取访问令牌>"

然而,当我尝试编写Delphi代码时:

RESTClient := TRESTClient.Create('https://api.dropboxapi.com');
RESTRequest := TRESTRequest.Create(RESTClient);
RESTClient.Authenticator := OAuth2.OAuth2Authenticator;
RESTRequest.Method := TRESTRequestMethod.rmPOST;
RESTRequest.Resource := '/2/users/get_current_account';

RESTRequest.Params.Clear;
RESTRequest.Params.AddHeader('Authorization', 'Bearer ' + OAuth2.AccessToken);
RESTRequest.AddParameter('Content-Type', 'text/plain; charset=dropbox-cors-hack', pkHTTPHEADER, [poDoNotEncode]);

RESTRequest.ClearBody; //显然这不会传送一个完全空的请求体

RESTRequest.OnHTTPProtocolError := ProcessException;    

try 
RESTRequest.Execute;
except
on E:...
end;

我得到了以下响应:

API函数“users/get_current_account”的调用中出现错误:请求体:无法将输入解码为JSON

我还尝试更改Content-Type行为以下两行:

RESTRequest.AddParameter('Content-Type', 'application/json', pkHTTPHEADER, [poDoNotEncode]);
RESTRequest.Params.AddBody('', TRESTContentType.ctAPPLICATION_JSON);

或以下两行:

RESTRequest.AddParameter('Content-Type', 'application/json', pkHTTPHEADER, [poDoNotEncode]);
RESTRequest.Params.AddBody('{}', TRESTContentType.ctAPPLICATION_JSON);

结果相同:

API函数“users/get_current_account”的调用中出现错误:请求体:无法将输入解码为JSON

我已经浪费了一天的时间尝试弄清楚,请求帮助解决这个问题...

英文:

Please help me figure out how to write Delphi Code correctly to match the outcome of running the Curl POST Command without specifying a body.

I am able to run with success the CURL command and it returns account information as expected:

    curl -X POST https://api.dropboxapi.com/2/users/get_current_account\
     --header &quot;Authorization: Bearer &lt;get access token&gt;&quot;

However, when I try to write Delphi code for it:

    RESTClient:=TRESTClient.Create(&#39;https://api.dropboxapi.com&#39;);
    RESTRequest:=TRESTRequest.Create(RESTClient);
    RESTClient.Authenticator:= OAuth2.OAuth2Authenticator;
    RESTRequest.Method := TRESTRequestMethod.rmPOST;
    RESTRequest.Resource := &#39;/2/users/get_current_account&#39;;

    RESTRequest.Params.Clear;
    RESTRequest.Params.AddHeader(&#39;Authorization&#39;,&#39;Bearer &#39;+OAuth2.AccessToken);
    RESTRequest.AddParameter(&#39;Content-Type&#39;, &#39;text/plain; charset=dropbox-cors-hack&#39;, pkHTTPHEADER,[poDoNotEncode]);

    RESTRequest.ClearBody; //aparently it doesn&#39;t translate into sending a completely empty body

    RESTRequest.OnHTTPProtocolError := ProcessException;    
    
    try 
    RESTRequest.Execute;
    except
    on E:...
    end;

I get the following Response:

    Error in call to API function &quot;users/get_current_account&quot;: request body: could not decode input as JSON

I also tried to change the Content-Type line with those 2 lines:

    RESTRequest.AddParameter(&#39;Content-Type&#39;, &#39;application/json&#39;, pkHTTPHEADER, [poDoNotEncode]);
    RESTRequest.Params.AddBody(&#39;&#39;,TRESTContentType.ctAPPLICATION_JSON);

Or with those 2 lines:

    RESTRequest.AddParameter(&#39;Content-Type&#39;, &#39;application/json&#39;, pkHTTPHEADER, [poDoNotEncode]);
    RESTRequest.Params.AddBody(&#39;{}&#39;,TRESTContentType.ctAPPLICATION_JSON);

The result is the same:

    Error in call to API function &quot;users/get_current_account&quot;: request body: could not decode input as JSON

I wasted already 1 day trying to figure it out, please help me with this...

答案1

得分: 0

在浪费了比我应该更多的时间后,我找到了答案:

RESTRequest.Params.AddHeader('Authorization', 'Bearer ' + OAuth2.AccessToken);
RESTRequest.AddParameter('Content-Type', 'application/json', pkHTTPHEADER, [poDoNotEncode]);
RESTRequest.AddBody('null', TRESTContentType.ctapplication_json);

希望这能帮助其他人不再浪费时间在这个问题上。

英文:

After wasting way more time than I had to, I found the answer:

RESTRequest.Params.AddHeader(&#39;Authorization&#39;,&#39;Bearer &#39;+OAuth2.AccessToken);
RESTRequest.AddParameter(&#39;Content-Type&#39;, &#39;application/json&#39;, pkHTTPHEADER, [poDoNotEncode]);
RESTRequest.AddBody(&#39;null&#39;,TRESTContentType.ctapplication_json);

Hope it will help others to not waste any more time on this issue..

huangapple
  • 本文由 发表于 2023年7月7日 02:12:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定