send HTTP Get request using Curl in c

妖精的绣舞 提交于 2019-12-10 19:49:14

问题


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


来源:https://stackoverflow.com/questions/27422918/send-http-get-request-using-curl-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!