Search code examples
jsonrestcurlpostdropbox

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


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...


Solution

  • 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..