1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #include <curl/curl.h>
33 WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
35 size_t realsize = size * nmemb;
36 struct MemoryStruct *mem = (struct MemoryStruct *)userp;
38 mem->memory = realloc(mem->memory, mem->size + realsize + 1);
39 if(mem->memory == NULL) {
41 printf("not enough memory (realloc returned NULL)\n");
45 memcpy(&(mem->memory[mem->size]), contents, realsize);
46 mem->size += realsize;
47 mem->memory[mem->size] = 0;
56 struct MemoryStruct chunk;
57 static const char *postthis="Field=1&Field=2&Field=3";
59 chunk.memory = malloc(1); /* will be grown as needed by realloc above */
60 chunk.size = 0; /* no data at this point */
62 curl_global_init(CURL_GLOBAL_ALL);
63 curl = curl_easy_init();
66 curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.org/");
68 /* send all data to this function */
69 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
71 /* we pass our 'chunk' struct to the callback function */
72 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
74 /* some servers don't like requests that are made without a user-agent
75 field, so we provide one */
76 curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
78 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
80 /* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
82 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
84 /* Perform the request, res will get the return code */
85 res = curl_easy_perform(curl);
86 /* Check for errors */
88 fprintf(stderr, "curl_easy_perform() failed: %s\n",
89 curl_easy_strerror(res));
93 * Now, our chunk.memory points to a memory block that is chunk.size
94 * bytes big and contains the remote file.
96 * Do something nice with it!
98 printf("%s\n",chunk.memory);
102 curl_easy_cleanup(curl);
106 /* we're done with libcurl, so clean it up */
107 curl_global_cleanup();