Search code examples
delphicurl

Convert cURL to Delphi


I am using Delphi 10.2. I have generated this cURL statement using the tools supplied by the API provider and in that environment it runs successfully and returns a result.

curl --request POST \
  --url https://api.netradyne.com/driveri/v1/tenants/N251887947004171/driver/assignment \
  --header 'Accept: application/json' \
  --header 'Accept-Language: en-US' \
  --header 'Authorization: Bearer <access token replaced>' \
  --header 'Content-Type: application/json' \
  --data '{
  "driverID": "48625329",
  "vehicleID": "1393",
  "action": "login",
  "startTimestamp": 1720439154012,
  "endTimestamp": 1720439208018
}'

I have successfully reproduced one other cURL statement using the REST Debugger, but the best I can get is a response that starts off with "400:Bad Request - 0 bytes of data returned...".

The REST Debugger file is:

{
    "method": "POST",
    "url": "https://api.netradyne.com/driveri/v1/tenants/N251887947004171/driver/assignment",
    "resource": "",
    "contenttype": "application/json",
    "auth": {
        "method": "OAUTH2",
        "username": "",
        "passwordkey": "",
        "password": "",
        "clientid": "",
        "clientsecret": "",
        "authcode": "",
        "accesstoken": "Bearer <access token replaced>",
        "accesstokensecret": "",
        "requesttoken": "",
        "requesttokensecret": "",
        "refreshtoken": "",
        "signaturemethod": "",
        "responsetype": "code",
        "endpointauth": "",
        "endpointaccesstoken": "",
        "endpointrequesttoken": "",
        "endpointredirect": "",
        "authscope": ""
    },
    "parameters": [
        {
            "name": "Authorization",
            "value": "Bearer <access token replaced>",
            "kind": "HEADER",
            "encode": false
        },
        {
            "name": "Accept",
            "value": "application/json",
            "kind": "HEADER",
            "encode": false
        },
        {
            "name": "Content-Type",
            "value": "application/json",
            "kind": "HEADER",
            "encode": false
        },
        {
            "name": "data",
            "value": "{\"driverID\": \"48625329\",\"vehicleID\": \"1393\",\"action\": \"login\",\"startTimestamp\": 1720439154012,\"endTimestamp\": 1720439208018}",
            "kind": "BODY",
            "encode": false
        }
    ],
    "body": ""
}

Any thoughts on how to get Delphi to produce what this API is looking for?


Solution

  • I removed the "Bearer" from the accesstoken field and moved the JSON from the parameters to the body. The JSON that the REST Debugger produced included "body": "ZGF0YT17ImRyaXZlcklEIjogIjQ4NjI1MzI5IiwidmVoaWNsZUlEIjogIjEzOTMiLCJhY3Rpb24iOiAibG9naW4iLCJzdGFydFRpbWVzdGFtcCI6IDE3MjA0MzkxNTQwMTIsImVuZFRpbWVzdGFtcCI6IDE3MjA0MzkyMDgwMTh9". Running the request in the debugger generated a Windows alert sound and "(no response)".

    When I used the debugger to create Delphi components, I got a TRESTRequest where the last parameter in Params had the namebody, the kind pkREQUESTBODY and the value was blank. I populated the Value of the body parameter with the value of the "data" from the cURL statement and plunked down a button to execute the TRESTRequest and display the Content property of the the TRESTResponse. What I got was

    {"data":{"message":"Driver integration data received successfully."}}

    Which was the expected result from the cURL statement. Yay.

    Thanks for the help Remy