xref: /curl/docs/examples/postinmemory.c (revision f540e43b)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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 https://curl.se/docs/copyright.html.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 /* <DESC>
25  * Make an HTTP POST with data from memory and receive response in memory.
26  * </DESC>
27  */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <curl/curl.h>
32 
33 struct MemoryStruct {
34   char *memory;
35   size_t size;
36 };
37 
38 static size_t
WriteMemoryCallback(void * contents,size_t size,size_t nmemb,void * userp)39 WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
40 {
41   size_t realsize = size * nmemb;
42   struct MemoryStruct *mem = (struct MemoryStruct *)userp;
43 
44   char *ptr = realloc(mem->memory, mem->size + realsize + 1);
45   if(!ptr) {
46     /* out of memory! */
47     printf("not enough memory (realloc returned NULL)\n");
48     return 0;
49   }
50 
51   mem->memory = ptr;
52   memcpy(&(mem->memory[mem->size]), contents, realsize);
53   mem->size += realsize;
54   mem->memory[mem->size] = 0;
55 
56   return realsize;
57 }
58 
main(void)59 int main(void)
60 {
61   CURL *curl;
62   CURLcode res;
63   struct MemoryStruct chunk;
64   static const char *postthis = "Field=1&Field=2&Field=3";
65 
66   chunk.memory = malloc(1);  /* grown as needed by realloc above */
67   chunk.size = 0;    /* no data at this point */
68 
69   curl_global_init(CURL_GLOBAL_ALL);
70   curl = curl_easy_init();
71   if(curl) {
72     curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.org/");
73 
74     /* send all data to this function  */
75     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
76 
77     /* we pass our 'chunk' struct to the callback function */
78     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
79 
80     /* some servers do not like requests that are made without a user-agent
81        field, so we provide one */
82     curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
83 
84     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
85 
86     /* if we do not provide POSTFIELDSIZE, libcurl calls strlen() by itself */
87     curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
88 
89     /* Perform the request, res gets the return code */
90     res = curl_easy_perform(curl);
91     /* Check for errors */
92     if(res != CURLE_OK) {
93       fprintf(stderr, "curl_easy_perform() failed: %s\n",
94               curl_easy_strerror(res));
95     }
96     else {
97       /*
98        * Now, our chunk.memory points to a memory block that is chunk.size
99        * bytes big and contains the remote file.
100        *
101        * Do something nice with it!
102        */
103       printf("%s\n",chunk.memory);
104     }
105 
106     /* always cleanup */
107     curl_easy_cleanup(curl);
108   }
109 
110   free(chunk.memory);
111   curl_global_cleanup();
112   return 0;
113 }
114