Reputation: 14448
I am sending JSON string in JSON format using curl to post JSON data.
I've used following curl command:
curl -H "Content-Type: application/json" -d '{ "Mode":"Auto","Type":"Single","IP":"9.9.9.9","FirmwarePath":""}' http://1.1.1.1/cgi-bin/update_firmware.cgi
Output of the above command:
<h1>{ "Mode":"Auto","Type":"Single","IP":"9.9.9.9","FirmwarePath":""} </h1><h1>Check Mode</h1><h1>Auto</h1><h1>Single</h1><h1 /><h1>Here Mode</h1><h1>{ "Mode":"Auto","Type":"Single","IP":"9.9.9.9","FirmwarePath":""}</h1><h1>Check Input</h1><h1>Inside Firmware updates</h1><h1>In CheckFirmware</h1><h1>curl -s --digest --basic -u root:UOIYTm7Aw52Z "http://9.9.9.9/axis-cgi/operator/param.cgi?action=list&group=Properties.Firmware.Version" | cut -d'=' -f2</h1><h1>cam Firm:5.50.4.2
</h1><h1>DB Firm: </h1>Status: 200
Content-Type: application/json; charset=ISO-8859-1
{"ReqStatus":{"Status":"Firmware is upto date or Firmaware update mode is off.","IP":null}}
So, it works fine & gives me an expected output.
Now I want this output stored in text file. So, I've made C program to achieve that:
#include <stdio.h>
#include <curl/curl.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main(void) {
CURL *curl;
FILE *fp = fopen("request.txt","w");
CURLcode res;
curl = curl_easy_init();
char* jsonObj = "{\"Mode\":\"Auto\",\"type\":\"Single\",\"IP\":\"9.9.9.9\",\"FirmwarePath\":\"""\"}";
puts(jsonObj);
struct curl_slist *headers = NULL;
curl_slist_append(headers, "Accept: application/json");
curl_slist_append(headers, "Content-Type: application/json");
curl_slist_append(headers, "charsets: utf-8");
curl_easy_setopt(curl, CURLOPT_URL, "http://1.1.1.1/cgi-bin/update_firmware.cgi");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonObj);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA,fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
But after executing this C program when I open request.txt file I see unexpected output.
Contents of request.txt file:
<h1>Software error:</h1>
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)") at /var/www/cgi-bin/update_firmware.cgi line 496
<p>
For help, please send mail to the webmaster (<a href="mailto:root@localhost">root@localhost</a>), giving this error message
and the time and date of the error.
</p>
JSON data that I want to send is:
{
"Mode":"Auto",
"Type":"Single",
"IP":"9.9.9.9",
"FirmwarePath":""
}
What I am doing wrong here ?
Upvotes: 1
Views: 212
Reputation: 58234
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
Remove that option. Your command line doesn't set any custom method but uses POST (implied by -d
).
There's some confusion among the quotes at the end of your jsonObj string, it should probably be:
char* jsonObj = "{\"Mode\":\"Auto\",\"type\":\"Single\",\"IP\":\"9.9.9.9\",\"FirmwarePath\":\"\" }";
Upvotes: 3