summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-17 17:10:03 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-17 17:10:03 +0800
commita9667989e8bb66035585320c402b589328c14f23 (patch)
treeabfb5d0c14160677ce86cb132eac9a8aa1c21093
parent28dd96a0da198e3919b9e874773c57cb56a40157 (diff)
POST for Fetch API
-rw-r--r--request.h27
1 files 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);