Home > Software engineering >  How to Insert data to CloudTables database using cURL/HTTP in PHP? Error: API Key is required
How to Insert data to CloudTables database using cURL/HTTP in PHP? Error: API Key is required

Time:11-05

I'm inserting a row to the CloudTables database using cURL request. following is the sample cURL request available on their [documentations][1]:

curl \
    -X POST \
    -d key=:apiKey \
    https://sub-domain.cloudtables.io/api/1/dataset/:id

Where

  • :apiKey is the API key to use for access (see below)
  • :id is the dataset id (a UUID),

And below is my PHP Code:

    $post = array(
        'clientId' => $user_id,
        'clientName' => $user_email,
        'dp-01' => $user_id,
        'dp-02' => $user_type,
        'dp-03' => $fullname,
        'dp-04' => $address,
    );

    $ch = curl_init('https://sub-domain.cloudtables.io/api/1/dataset/my-dataset-id');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('key: my-api-key'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
    curl_close($ch);

But every time response says:

[
  {
     "msg":"API key is required",
     "name":"key"
  }
]

Why!!! What is the right way to send API key?

I also tried sending API key in $post array and in URL, but getting same response. [1]: https://cloudtables.com/docs/cloud/api/rest/post-dataset

CodePudding user response:

Passing an array for CURLOPT_POSTFIELDS, will make it send a multipart/form-data request - whereas the API apparently expects application/x-www-form-urlencoded.

Use curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); instead

  • Related