From a9667989e8bb66035585320c402b589328c14f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Thu, 17 Jun 2021 17:10:03 +0800 Subject: POST for Fetch API --- request.h | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/request.h b/request.h index 7960561..e3b81c8 100644 --- a/request.h +++ b/request.h @@ -109,8 +109,31 @@ static inline void request(void (*handler)(icclient_fetch_t *), void *callback, #ifdef __EMSCRIPTEN__ if (handler) attr.onsuccess = handler; - attr.userData = callback; - strcpy(attr.requestMethod, "GET"); + char *post = NULL; + if (body) { + size_t length = 0; + post = malloc(1); + memset(post, '\0', 1); + for (size_t i = 0; i < body->num_pairs; i++) { + struct pair pair = body->pairs[i]; + if (!pair.value) + continue; + length += strlen(pair.key) + strlen(pair.value) + (i ? 1 : 0) + 1; + post = realloc(post, length + 1); + if (i) + strcat(post, "&"); + sprintf(post, "%s%s=%s", post, pair.key, pair.value); + } + strcpy(attr.requestMethod, "POST"); + const char *headers[] = { "Content-Type", "application/x-www-form-urlencoded", NULL }; + attr.requestHeaders = headers; + attr.requestData = post; + attr.requestDataSize = length + 1; + attr.userData = post; + } else { + strcpy(attr.requestMethod, "GET"); + attr.userData = callback; + } emscripten_fetch(&attr, url); #else curl_easy_setopt(curl, CURLOPT_URL, url); -- cgit v1.2.3