2017-11-22 36 views
1

JSON形式でJSON形式のデータを送信するためにcurlを使用してJSON形式でJSON文字列を送信しています。私はcurlコマンド以下の使用してきましたCで不正な形式のJSON文字列エラー

:上記のコマンドの

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 

出力:

<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}} 

だから、それは、&は私に期待される出力を与える正常に動作します。

今、この出力をテキストファイルに保存します。だから、私はそれを達成するためにCプログラムを作りました:

#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; 
} 

しかし、私は、私は予想外の出力を参照してくださいrequest.txtファイルを開くと、このCのプログラムを実行した後。 request.txtファイルの

内容:私は送信したい

<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:[email protected]">[email protected]</a>), giving this error message 
and the time and date of the error. 

</p> 

JSONデータは次のとおりです。

{ 
    "Mode":"Auto", 
    "Type":"Single", 
    "IP":"9.9.9.9", 
    "FirmwarePath":"" 
} 

私はここで間違って何をしているのですか?

答えて

3
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); 

このオプションを削除します。コマンドラインでカスタムメソッドが設定されず、POST(-dが暗黙指定されています)が使用されます。あなたのjsonObj文字列の末尾の引用符の中でいくつかの混乱があります

、それはおそらく次のようになります。

char* jsonObj = "{\"Mode\":\"Auto\",\"type\":\"Single\",\"IP\":\"9.9.9.9\",\"FirmwarePath\":\"\" }"; 
+0

それはまだ同じエラーが表示されます。 – Destructor

+2

'--libcurl template.c'をcurlコマンドラインに追加し、あなたが見逃してしまったものがあるかどうか確認してください –

関連する問題