bqn-curl/README.org

53 lines
1.5 KiB
Org Mode

* BQN HTTP library
FFI bindings to [[https://curl.se/libcurl/][libcurl]] for [[https://mlochbaum.github.io/BQN/][BQN]].
** Configuration
Set the location of the =libcurl.so= library in [[./config.bqn][config.bqn]].
** Making requests
Only GET and POST requests are implemented at this time.
#+begin_src bqn
Get,Post•Import"curl.bqn"
#+end_src
Simple GET requests:
#+begin_src bqn
rGet"https://httpbin.org/get"
#+end_src
You can pass headers as a left argument:
#+begin_src bqn
r"Content-Type: application/json"Get"https://httpbin.org/get"
#+end_src
Arguments, port number, etc, can be included in the URL.
For POST requests, pass data as an additional string parameter. It
will not be converted or encoded in any way.
#+begin_src bqn
r"Content-Type: application/json"Post"https://httpbin.org/post""{""key"": ""value""}"
#+end_src
** Response objects
The response object is a namespace with the following elements:
- ~code~: the response code.
- ~headers~: the response headers as a single string, separated by newlines.
- ~content~: the response content, as raw bytes.
- ~time~: the time the request took, in seconds.
If your endpoint returns text, you can use ~FromBytes~ from
[[https://github.com/mlochbaum/bqn-libs/blob/master/strings.bqn][bqn-libs/strings.bqn]] to decode UTF-8.
** Tests
Run a local [[https://httpbin.org/][httpbin.org]] instance with Docker:
#+begin_src sh
docker run -p 8080:80 kennethreitz/httpbin
#+end_src
The tests can be run with =bqn tests.bqn=.