1

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

4
  • 1
    curl -d is not for setting values in the header, it's for data (curl.se/docs/manpage.html#-d). Try putting key and apiKey into the $post data. Commented Nov 2, 2021 at 18:25
  • @Alan already tried putting "key" => "APIKey" into $post but still getting same error! Commented Nov 2, 2021 at 18:48
  • 1
    Passing an array for CURLOPT_POSTFIELDS, will make it send a multipart/form-data request - maybe the API doesn't like those? Try curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));, and see if that changes anything. Commented Nov 3, 2021 at 9:35
  • Thanks @CBroe; It's working now! I request you to plz post this as answer so I can mark it as solution. so it can help for others. ` $post = array( 'key' => 'my-api-key', 'clientId' => $user_id, 'clientName' => $user_email, 'dp-01' => $user_id, 'dp-02' => $user_type, 'dp-03' => $fullname, ); $ch = curl_init('sub-domain.cloudtables.io/api/1/dataset/my-dataset-id'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); $response = curl_exec($ch); curl_close($ch); ` Commented Nov 5, 2021 at 5:47

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.