컬은

2017-03-13 7 views
0

현재 내가 libcurl에를 사용하여 JSON 형식으로 HTTP 넣어 요청을 보낼 수있는 방법이 있나요 컬은

ArangoDB

와 상호 작용하는 프로그램을 쓰고 있어요? 컬에서

내 현재의 명령은 내가 위의 요청을 보낼 수있는 동등한 libcurl에 코드를 알고 싶습니다

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

입니다. 감사합니다

+0

콘텐츠 유형 헤더를 설정 했습니까? 예 : 'curl -v -H "Accept : application/json"-H "내용 유형 : application/json"' –

답변

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

이것은 내가 생각해내는 해결책입니다. 추가/변경 하시길 바랍니다.