GithubHelp home page GithubHelp logo

dlozeve / bqn-curl Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 23 KB

BQN HTTP library, from libcurl FFI bindings

License: BSD 3-Clause "New" or "Revised" License

bqn ffi http http-client http-requests libcurl libcurl-bindings

bqn-curl's Introduction

BQN HTTP library

FFI bindings to libcurl for BQN.

Configuration

Set the location of the libcurl.so library in config.bqn.

Usage

Simple API

Only GET and POST requests are implemented at this time.

Get,Post⟩←•Import"curl.bqn"

Simple GET requests:

rGet"https://httpbin.org/get"

You can pass headers as a left argument:

r←⟨"Content-Type: application/json"Get"https://httpbin.org/get"

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.

r←⟨"Content-Type: application/json"Post"https://httpbin.org/post""{""key"": ""value""}"

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.
  • redirectCount: the number of redirects.

If your endpoint returns text, you can use FromBytes from bqn-libs/strings.bqn to decode UTF-8.

Advanced API

For more fine-grained control on request parameters, you can use the advanced API, which closely mirrors the libcurl API structure.

A session object is created with OpenSession, and can be reused to speed up successive requests by reusing an existing connection.

Create a request by modifying the session object. Each of the functions take the session as their right argument, and configuration data as their left argument. They modify the session in-place, but also return it so that they can be chained easily.

  • url SetURL session: the request URL.
  • headers SetHeaders session: the request headers, as a list of strings.
  • SetVerbose session: log verbose request data to standard output.
  • n SetTimeout session and n SetTimeoutms timeout: the request timeout, in seconds or in milliseconds.
  • data SetData session: use a POST request and set the data to send, as a string.

Finally, perform the actual request with Perform, returning the response object as above.

The session object can then be reused for subsequent requests, modifying request parameters as needed. It can be reset to its default configuration using ResetSession, while preserving open connections and caches.

A session is terminated, and its memory freed, with CloseSession.

Full example:

OpenSession,CloseSession,
  SetURL,SetHeaders,SetTimeoutms,
  Perform,
⟩←•Import"curl.bqn"

session←⟨"User-Agent: myapp"SetHeaders OpenSession @
r1Perform "https://httpbin.org/status/418"SetURL session
•Show r1.code
r2Perform 500 SetTimeoutms "https://httpbin.org/ip"SetURL session
•Out r2.content

CloseSession session

Running tests

Run a local httpbin.org instance with Docker:

docker run -p 8080:80 kennethreitz/httpbin

The tests can be run with bqn tests.bqn.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.