From 4898094229ecee8061896280afffbe74bae717e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=20=EA=A6=AB=EA=A6=B6=20=EA=A6=8F=EA=A7=80?= =?UTF-8?q?=EA=A6=A6=EA=A6=BF=20=EA=A6=A7=20=EA=A6=AE=20=EA=A6=91=20?= =?UTF-8?q?=EA=A6=A9=20=EA=A6=AD=EA=A7=80?= Date: Thu, 19 Sep 2019 16:43:18 +0800 Subject: Post content builder inline function --- post.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 post.h (limited to 'post.h') diff --git a/post.h b/post.h new file mode 100644 index 0000000..ded474e --- /dev/null +++ b/post.h @@ -0,0 +1,30 @@ +#include "request.h" + +inline void post(const char *path, const char *pairs[], size_t n) +{ + size_t length = 0; + for (size_t i = 0; i < n; i += 2) { + const char *pair = pairs[i]; + if (pair && strcmp(pair, "")) + length += strlen(pair) + strlen(++pair) + 3; + } + + char content[length + 1]; + memset(content, 0, strlen(content)); + for (size_t i = 0; i < n; i += 2) { + const char *pair = pairs[i]; + if (pair && strcmp(pair, "")) + sprintf(content, "%s%s: %s\n", content, pairs[i + 1] + , pair); + } + + struct curl_httppost *post, *last = NULL; + curl_formadd(&post, &last + , CURLFORM_COPYNAME, "content" + , CURLFORM_PTRCONTENTS, content + , CURLFORM_END); + last = NULL; + request(path, "", NULL, NULL, post); + curl_formfree(post); + post = NULL; +} -- cgit v1.2.3