diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-17 17:10:03 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-17 17:10:03 +0800 |
commit | a9667989e8bb66035585320c402b589328c14f23 (patch) | |
tree | abfb5d0c14160677ce86cb132eac9a8aa1c21093 /request.h | |
parent | 28dd96a0da198e3919b9e874773c57cb56a40157 (diff) |
POST for Fetch API
Diffstat (limited to 'request.h')
-rw-r--r-- | request.h | 27 |
1 files changed, 25 insertions, 2 deletions
@@ -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); |