deislabs / wagi

Write HTTP handlers in WebAssembly with a minimal amount of work
Apache License 2.0
889 stars 44 forks source link

Way to extract content body data from request? #121

Closed dills122 closed 3 years ago

dills122 commented 3 years ago

I started playing around with some of the example repos that are available, specifically the AssemblyScript one, and was wondering if there was a way to pull data from the body of a request like the example below or would you need to send the data in via query parameters?

curl -d 'hello world' -H 'Content-Type: text/plain' http://localhost:3000/as-hello

It looks like the CONTENT_LENGTH is updated correctly, but I dont see an env var available for request body data.

itowlson commented 3 years ago

The request body is provided on stdin. Not sure what the idiom is in AS for reading that I'm afraid.

dills122 commented 3 years ago

I was able to figure it out. It looks like it was due to the as-wasi version being out of date and an issue with reading from Stdin in that version.

Once I upgraded both as-wasi and assemblyscript to latest I was able to read the request body with the following:

const reqBody= Descriptor.Stdin.readString();

Console.log(defstr(reqBody, "EMPTY"));

Thread with some info on it https://github.com/bytecodealliance/wasmtime/issues/2373