send HTTP Get request using Curl in c

2019-07-07 05:24发布

问题:

Please help me to implement an HTTP Get request using curl in C.

I need to hit URL with parameters like https://www.googleapis.com/tasks/v1/users?name=pradeep&lastname=singla I used CURLOPT_HTTPHEADER to set parameters with Header but without success. I implemented it like

struct curl_slist* contentheader = NULL;    

contentheader = curl_slist_append(contentheader, "name=pradeep");
contentheader = curl_slist_append(contentheader, "lastname=singla");

curl_easy_setopt(curl, CURLOPT_URL, "https://www.googleapis.com/tasks/v1/users");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, contentheader);
curl_easy_perform(curl); 

In this case an error occured like "no correct APi request". So I thought that I can use

char *charff = "name=pradeep&lastname=singla";    
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, charfff);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_perform(curl);

but the same error is occurring.

Can anyone please help me ? I can put my request for both POST and GET method because server method may change anytime.

回答1:

The URL is just a URL even with "parameters" and you set it in full with CURLOPT_URL.

They're not headers and they're not postfields.

CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, 
    "https://www.googleapis.com/tasks/v1/users?name=pradeep&lastname=singla");
curl_easy_perform(curl);


标签: c curl libcurl