-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
44 lines (36 loc) · 1.19 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <string.h>
#include <emscripten/fetch.h>
#include <janet.h>
static Janet cfun_httpget(int32_t argc, Janet *argv)
{
janet_fixarity(argc, 1);
const char *url = janet_getcstring(argv, 0);
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_REPLACE;
emscripten_fetch_t *fetch = emscripten_fetch(&attr, url);
if (fetch->status == 200)
{
JanetString res = janet_string((const uint8_t *)fetch->data, fetch->numBytes);
emscripten_fetch_close(fetch);
return janet_wrap_string(res);
}
else
{
printf("HTTP failure status code: %d.\n", fetch->status);
}
emscripten_fetch_close(fetch);
return janet_wrap_nil();
}
int main(int argc, const char *argv[])
{
janet_init();
JanetTable *env = janet_core_env(NULL);
janet_def(env, "httpget", janet_wrap_cfunction(cfun_httpget), "make HTTP request");
const char *code = "(print (httpget `https://httpbin.org/anything`))";
janet_dostring(env, code, "main", NULL);
janet_deinit();
return 0;
}