2013-09-29 06:12:30 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2013-09-29 06:12:30 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2013-09-29 06:12:30 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2021-10-31 23:34:44 +08:00
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2013-09-29 06:12:30 +08:00
|
|
|
***************************************************************************/
|
2016-01-04 22:34:05 +08:00
|
|
|
/* <DESC>
|
2022-10-26 14:59:35 +08:00
|
|
|
* Make an HTTP POST with data from memory and receive response in memory.
|
2016-01-04 22:34:05 +08:00
|
|
|
* </DESC>
|
|
|
|
*/
|
2013-09-29 06:12:30 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
struct MemoryStruct {
|
|
|
|
char *memory;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
|
|
|
{
|
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
|
|
|
|
|
2018-09-15 13:55:11 +08:00
|
|
|
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
|
|
|
|
if(!ptr) {
|
2013-09-29 06:12:30 +08:00
|
|
|
/* out of memory! */
|
|
|
|
printf("not enough memory (realloc returned NULL)\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:55:11 +08:00
|
|
|
mem->memory = ptr;
|
2013-09-29 06:12:30 +08:00
|
|
|
memcpy(&(mem->memory[mem->size]), contents, realsize);
|
|
|
|
mem->size += realsize;
|
|
|
|
mem->memory[mem->size] = 0;
|
|
|
|
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode res;
|
|
|
|
struct MemoryStruct chunk;
|
2017-09-10 05:09:06 +08:00
|
|
|
static const char *postthis = "Field=1&Field=2&Field=3";
|
2013-09-29 06:12:30 +08:00
|
|
|
|
2024-02-27 19:29:27 +08:00
|
|
|
chunk.memory = malloc(1); /* grown as needed by realloc above */
|
2013-09-29 06:12:30 +08:00
|
|
|
chunk.size = 0; /* no data at this point */
|
|
|
|
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if(curl) {
|
2018-09-23 17:20:26 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.org/");
|
2013-09-29 06:12:30 +08:00
|
|
|
|
|
|
|
/* send all data to this function */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
|
|
|
|
|
|
|
/* we pass our 'chunk' struct to the callback function */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
|
|
|
|
|
2021-10-31 23:34:44 +08:00
|
|
|
/* some servers do not like requests that are made without a user-agent
|
2013-09-29 06:12:30 +08:00
|
|
|
field, so we provide one */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
|
|
|
|
|
2024-02-27 19:29:27 +08:00
|
|
|
/* if we do not provide POSTFIELDSIZE, libcurl calls strlen() by itself */
|
2013-09-29 06:12:30 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
|
|
|
|
|
2024-02-27 19:29:27 +08:00
|
|
|
/* Perform the request, res gets the return code */
|
2013-09-29 06:12:30 +08:00
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
/* Check for errors */
|
|
|
|
if(res != CURLE_OK) {
|
|
|
|
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
|
|
|
curl_easy_strerror(res));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* Now, our chunk.memory points to a memory block that is chunk.size
|
|
|
|
* bytes big and contains the remote file.
|
|
|
|
*
|
|
|
|
* Do something nice with it!
|
|
|
|
*/
|
|
|
|
printf("%s\n",chunk.memory);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
2019-12-24 14:06:47 +08:00
|
|
|
|
2019-02-14 18:53:02 +08:00
|
|
|
free(chunk.memory);
|
2019-12-24 14:06:47 +08:00
|
|
|
curl_global_cleanup();
|
2013-09-29 06:12:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|