英文:
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 "Authorization: Bearer <get access token>"
However, when I try to write Delphi code for it:
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;
I get the following Response:
Error in call to API function "users/get_current_account": request body: could not decode input as JSON
I also tried to change the Content-Type line with those 2 lines:
RESTRequest.AddParameter('Content-Type', 'application/json', pkHTTPHEADER, [poDoNotEncode]);
RESTRequest.Params.AddBody('',TRESTContentType.ctAPPLICATION_JSON);
Or with those 2 lines:
RESTRequest.AddParameter('Content-Type', 'application/json', pkHTTPHEADER, [poDoNotEncode]);
RESTRequest.Params.AddBody('{}',TRESTContentType.ctAPPLICATION_JSON);
The result is the same:
Error in call to API function "users/get_current_account": 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('Authorization','Bearer '+OAuth2.AccessToken);
RESTRequest.AddParameter('Content-Type', 'application/json', pkHTTPHEADER, [poDoNotEncode]);
RESTRequest.AddBody('null',TRESTContentType.ctapplication_json);
Hope it will help others to not waste any more time on this issue..
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论