postinmemory.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * Make a HTTP POST with data from memory and receive response in memory.
00024  * </DESC>
00025  */
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <curl/curl.h>
00030 
00031 struct MemoryStruct {
00032   char *memory;
00033   size_t size;
00034 };
00035 
00036 static size_t
00037 WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
00038 {
00039   size_t realsize = size * nmemb;
00040   struct MemoryStruct *mem = (struct MemoryStruct *)userp;
00041 
00042   mem->memory = realloc(mem->memory, mem->size + realsize + 1);
00043   if(mem->memory == NULL) {
00044     /* out of memory! */
00045     printf("not enough memory (realloc returned NULL)\n");
00046     return 0;
00047   }
00048 
00049   memcpy(&(mem->memory[mem->size]), contents, realsize);
00050   mem->size += realsize;
00051   mem->memory[mem->size] = 0;
00052 
00053   return realsize;
00054 }
00055 
00056 int main(void)
00057 {
00058   CURL *curl;
00059   CURLcode res;
00060   struct MemoryStruct chunk;
00061   static const char *postthis="Field=1&Field=2&Field=3";
00062 
00063   chunk.memory = malloc(1);  /* will be grown as needed by realloc above */
00064   chunk.size = 0;    /* no data at this point */
00065 
00066   curl_global_init(CURL_GLOBAL_ALL);
00067   curl = curl_easy_init();
00068   if(curl) {
00069 
00070     curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.org/");
00071 
00072     /* send all data to this function  */
00073     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
00074 
00075     /* we pass our 'chunk' struct to the callback function */
00076     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
00077 
00078     /* some servers don't like requests that are made without a user-agent
00079        field, so we provide one */
00080     curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
00081 
00082     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
00083 
00084     /* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
00085        itself */
00086     curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
00087 
00088     /* Perform the request, res will get the return code */
00089     res = curl_easy_perform(curl);
00090     /* Check for errors */
00091     if(res != CURLE_OK) {
00092       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00093               curl_easy_strerror(res));
00094     }
00095     else {
00096       /*
00097        * Now, our chunk.memory points to a memory block that is chunk.size
00098        * bytes big and contains the remote file.
00099        *
00100        * Do something nice with it!
00101        */
00102       printf("%s\n",chunk.memory);
00103     }
00104 
00105     /* always cleanup */
00106     curl_easy_cleanup(curl);
00107 
00108     free(chunk.memory);
00109 
00110     /* we're done with libcurl, so clean it up */
00111     curl_global_cleanup();
00112   }
00113   return 0;
00114 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06