Hello,
I am experimenting with js fetch API in Nim language (devel, jsfetch is experimental).
proc ajaxOptions(metoda: HttpMethod = HttpGet): FetchOptions =
let headers = newHeaders()
headers.add("key", "value")
result = newFetchOptions(
metod = metoda,
mode = fmNoCors,
cache = fchNoCache,
credentials = fcOmit,
headers = headers,
redirect = frFollow,
referrerPolicy = frpNoReferrer,
body = "".cstring,
keepalive = false
# referrer = "client".cstring,
# integrity = "".cstring
)
proc ajaxGet*(url: cstring):Future[Response] {.async.} =
result = url.fetch ajaxOptions(metoda = HttpGet)
proc ajaxPost*(url: cstring):Future[Response] {.async.} =
# result = url.fetch ajaxOptions(metoda = HttpPost)
let options = ajaxOptions(metoda = HttpPost)
result = url.fetch options
When I call fetch without FetchOptions parameter, url.fetch() the query works well.
When I add options fetch(url, options), there is an error running compiled javascript.
Uncaught (in promise) TypeError: Window.fetch: 'headers' member of RequestInit could not be converted to any of: sequence<sequence<ByteString>>, record<ByteString, ByteString>.
ajaxPost_1224736842 http://localhost:8081/wedos/app.js:5016
example_822083600 http://localhost:8081/wedos/app.js:5085
onLoad_822083597 http://localhost:8081/wedos/app.js:5145
EventListener.handleEvent* http://localhost:8081/wedos/app.js:5157
app.js:5016:25
example_822083600 http://localhost:8081/wedos/app.js:5131
AsyncFunctionThrow self-hosted:642
(Async: async)
onLoad_822083597 http://localhost:8081/wedos/app.js:5145
(Async: EventListener.handleEvent)
<anonymous> http://localhost:8081/wedos/app.js:5157
Where is the problem?
thanks
Petr Živoslav Bolf
Solved. I think, that is it bug in js compiler.
result.headers is null. I I add result.headers = headers, its work.
proc ajaxOptions(metoda: HttpMethod = HttpGet): FetchOptions =
let headers = newHeaders()
headers.add("key", "value")
result = newFetchOptions(
metod = metoda,
mode = fmNoCors,
cache = fchNoCache,
credentials = fcOmit,
headers = headers,
redirect = frFollow,
referrerPolicy = frpNoReferrer,
body = "".cstring,
keepalive = false
# referrer = "client".cstring,
# integrity = "".cstring
)
# if I add this line, it works and return headers with correct value
result.headers = headers
Looks to me like an unintentional omission in std/jsfetch:
func newfetchOptions*(metod: HttpMethod; body: cstring;
mode: FetchModes; credentials: FetchCredentials; cache: FetchCaches; referrerPolicy: FetchReferrerPolicies;
keepalive: bool; redirect = frFollow; referrer = "client".cstring; integrity = "".cstring,
headers: Headers = newHeaders()): FetchOptions =
## Constructor for `FetchOptions`.
result = FetchOptions(
body: body, mode: cstring($mode), credentials: cstring($credentials), cache: cstring($cache), referrerPolicy: cstring($referrerPolicy),
keepalive: keepalive, redirect: cstring($redirect), referrer: referrer, integrity: integrity,
metod: (case metod
of HttpHead: "HEAD".cstring
of HttpGet: "GET".cstring
of HttpPost: "POST".cstring
of HttpPut: "PUT".cstring
of HttpDelete: "DELETE".cstring
of HttpPatch: "PATCH".cstring
else: "GET".cstring
)
)
newfetchOptions takes headers as a parameter but doesn't use it when initializing FetchOptions.