3

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
  • Why not simply add the parameters to the URL, like it's usually done? Like exactly what you have in the URL you show (https://www.googleapis.com/tasks/v1/users?name=pradeep&lastname=singla). Commented Dec 11, 2014 at 12:35

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

in future if the server HTTP method get changed to POST than i have to send parameter using POST method so i am thinking the way i don't need to worry in future for method. just need a solution that can handle both cases (GET and POST)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.