2017-03-13 2 views
0

Zur Zeit schreibe ich ein Programm mit ArangoDB zu interagierencurl Libcurl http setzte Anfrage im JSON-Format

Gibt es eine Möglichkeit HTTP PUT-Anfrage im JSON-Format mit libcurl zu schicken?

Mein aktueller Befehl in curl ist

curl -X PUT --data-binary @- --dump - --user "root:" http://localhost:8529/_db/myapp1/_api/simple/lookup-by-keys << EOF {"keys" : [ "FirstUser" ], "collection" : "five"} EOF 

Ich mag würde das Äquivalent Libcurl Code wissen, über die Anfrage zu senden. Danke

+0

Haben Sie den Content-Type-Header gesetzt? z.B. 'curl -v -H" Akzeptiere: application/json "-H" Content-type: application/json "' –

Antwort

0
//PUT Request 
#include <stdio.h> 
#include <string.h> 
#include <curl/curl.h> 

int main(int argc, char const *argv[]){ 
CURL *curl; 
CURLcode res; 
long http_code; 
static const char *jsonObj = //insert json here 

curl = curl_easy_init(); 
curl_global_init(CURL_GLOBAL_ALL); 

if(curl){ 

    curl_easy_setopt(curl, CURLOPT_URL, "//insert your url here"); 
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC); 
    curl_easy_setopt(curl, CURLOPT_USERPWD, "root:"); 

    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonObj); 


    //enable to spit out information for debugging 
    //curl_easy_setopt(curl, CURLOPT_VERBOSE,1L); 

    res = curl_easy_perform(curl); 

    if (res != CURLE_OK){ 
     fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); 
    } 

    printf("\nget http return code\n"); 
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); 
    printf("http code: %lu\n", http_code); 


    curl_easy_cleanup(curl); 
    curl_global_cleanup(); 

    } 
return 0; 
} 

Dies ist die Lösung, die ich mit kommen. Bitte fühlen Sie sich frei, hinzuzufügen/zu verbessern