将无正文的 curl POST转换为REST请求 Delphi 代码/为Dropbox API /get_current_account端点编写 Delphi 代码

vfhzx4xs  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(164)

请帮助我弄清楚如何正确地编写 Delphi 代码,以匹配运行Curl POST命令的结果,而不指定主体。
我能够成功运行CURL命令,它按预期返回帐户信息:

curl -X POST https://api.dropboxapi.com/2/users/get_current_account\
     --header "Authorization: Bearer <get access token>"

字符串
然而,当我尝试为它编写 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; //aparently it doesn't translate into sending a completely empty body

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


我得到以下回应:

Error in call to API function "users/get_current_account": request body: could not decode input as 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);


结果是一样的:

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


我已经浪费了1天试图弄清楚它,请帮助我与此...

6qfn3psc

6qfn3psc1#

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

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

字符串
希望它能帮助其他人不要在这个问题上浪费更多的时间。

相关问题