Home > Software engineering >  Dropbox API Post API gives response Something went wrong
Dropbox API Post API gives response Something went wrong

Time:02-04

I'm trying to use Dropbox API to get some metadata info of a file inside my dropbox.

Here is my post code

std::ostringstream stream;
    curl_slist* headers = NULL;

    CURL* curl = curl_easy_init();
    char data[50];
    snprintf(data, sizeof(data), "{\"path\":\"%s\"}", "/3.png");
    curl_easy_setopt(curl, CURLOPT_URL, "https://api.dropboxapi.com/2/files/get_metadata");
    headers = curl_slist_append(headers, "Authorization: <MY TOKEN>");
    headers = curl_slist_append(headers, "\"Content-Type\":\"application/json\"");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);
    /* size of the POST data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 30L);

    /* pass in a pointer to the data - libcurl will not copy */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, R"({"path":"/3.png"})"); //Tried data char array it doesn't work too.
    CURLcode res = curl_easy_perform(curl);
    if (res != CURLE_OK)
        return 0;

    /* convert stream to string format */
    std::string output = stream.str();
    curl_easy_cleanup(curl);

It returns HTTP 400 code

Thats the error I get after http post with curllib

<!DOCTYPE html>

<html>

<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Dropbox</title>

<link href="https://cfl.dropboxstatic.com/static/metaserver/static/css/error.css" rel="stylesheet" type="text/css"/>

<link rel="shortcut icon" href="https://cfl.dropboxstatic.com/static/metaserver/static/images/favicon.ico"/>

</head>

<body>

<div >

<img src="https://cfl.dropboxstatic.com/static/metaserver/static/images/illustration_catalog/sickbox-illo_m1.png" srcset="https://cfl.dropboxstatic.com/static/metaserver/static/images/illustration_catalog/[email protected] 2x" alt="Error: 5xx"/>

</div>

<div id="errorbox">

<h1>Error</h1>Something went wrong. Don't worry, your files are still safe and the Dropboxers have been notified. Check out our <a href="https://www.dropbox.com/help">Help Center</a> and <a href="https://forums.dropbox.com">forums</a> for help, or head back to <a href="https://www.dropbox.com/home">home</a>.

</div>

On PostMan it gives good response but in my C code it gives me error as you can see on above

Http web request body should be something like this

{
    "path": "/3.png"
}

Here is response that I get from PostMan

{
    ".tag": "file",
    "name": "3.png",
    "path_lower": "/3.png",
    "path_display": "/3.png",
    "id": "id:kVJQZOYYiMsAAAAAAAAABg",
    "client_modified": "2023-02-01T18:54:24Z",
    "server_modified": "2023-02-01T18:54:24Z",
    "rev": "015f3a7fa16a9c100000002912dfcc0",
    "size": 5160,
    "is_downloadable": true,
    "content_hash": "feb3516ea07bab6053eecc7367ee9c282846f7bc17751b2819b85aa0f3ddefb5"
}

CodePudding user response:

This line:

headers = curl_slist_append(headers, "\"Content-Type\":\"application/json\"");

Looks suspicous with all the inlined quotes around it.

Shouldn't it just be:

headers = curl_slist_append(headers, "Content-Type: application/json");

What you should do is get a tool like Fiddler, Charles, or similar HTTP capture proxy tool and compare the request/response sent by your application and compare it to the one sent by Postman. I think you'll see a discrepancy or two if the above doesn't fix it.

  • Related