Unexpected token 004 at position 0 when posting JSON object in C

188 Views Asked by At

Im having a problem when trying to send a JSON object that I created in C. The server is able to both send and receive data just fine. Im quite an amateur when it comes to C.

    static int main_curl(neardal_record* pRecord)
    {
    CURL *curl;
    CURLcode res;
    char * record = pRecord->representation;

    json_object *jobj = json_object_new_object(); 
    json_object *jstring = json_object_new_string(record);

    json_object_object_add(jobj,"id", jstring);


    curl = curl_easy_init();

    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "Accept: application/json");
    headers = curl_slist_append(headers, "Content-Type: application/json");
    headers = curl_slist_append(headers, "charset: utf-8");

    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.43.20:5000/api/swipes");

    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jobj);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcrp/0.1");
    printf ("The json object created:%s ",json_object_to_json_string(jobj));


    res = curl_easy_perform(curl);

    curl_easy_cleanup(curl);
    curl_global_cleanup();
    return res;
    }

When printing out the JSON object im getting this -> The json object created:{ "id": "hello, world" }

The server response is as follows

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Error</title>
    </head>
    <body>
    <pre>SyntaxError: Unexpected token  in JSON at position 0<br> &nbsp; &nbsp;at JSON.parse 
    (&lt;anonymous&gt;)<br> &nbsp; &nbsp;at createStrictSyntaxError 
    (/Users/backend/node_modules/body-parser/lib/types/json.js:158:10)<br> 
    &nbsp; &nbsp;at parse (/Users/backend/node_modules/body- 
    parser/lib/types/json.js:83:15)<br> &nbsp; &nbsp;at 
    /Users/backend/node_modules/body-parser/lib/read.js:121:18<br> &nbsp; 
    &nbsp;at invokeCallback (/Users/backend/node_modules/raw- 
    body/index.js:224:16)<br> &nbsp; &nbsp;at done 
    (/Users/backend/node_modules/raw-body/index.js:213:7)<br> &nbsp; 
    &nbsp;at IncomingMessage.onEnd (/Users/backend/node_modules/raw- 
    body/index.js:273:7)<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:333:22)<br> &nbsp; 
    &nbsp;at endReadableNT (_stream_readable.js:1201:12)<br> &nbsp; &nbsp;at 
    processTicksAndRejections (internal/process/task_queues.js:84:21)</pre>
    </body>
    </html>

When intercepting the request with Wireshark the Post looks like this POST request And the response error is this BAD REQUEST 400

Im almost certain that the error is the way i'm building my JSON object, but as aforementioned I am very new at C. If I hardcode a text instead of the object the request is a success. Any help would be much appreciated. Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

Managed to figure it out after all by changing this curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jobj); to this curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_object_to_json_string(jobj));

correct me if im wrong but im assuming that I cannot just send a JSON object without turning it into a String first.

Any further suggestions would be appreciated!