JSON3.jl layer for HTTP.jl

I was playing with some JSON based REST APIs, and I was starting to get tired of typing JSON3.read(response.body) all the time, so I came up with the following layer.

julia> module HTTPJSON3
           import HTTP, JSON3
           function json3_layer(handler)
               return function(req; kw...)
                   HTTP.setheader(req, "Content-Type" => "application/json")
                   r = handler(req; kw...)
                   HTTP.Response(r.status, r.headers, JSON3.read(r.body); r.request)
           function HTTP.Messages.bodysummary(obj::Union{JSON3.Object, JSON3.Array})
               io = IOBuffer()
               print(io, obj)
           HTTP.@client [json3_layer]

Is there a package that already does something like this for me?

1 Like

As far as I have been looking, I have not found something similar.

Would not make sense to add this as an Package Extension to HTTP?


A little while ago I started an experimental package for creating REST API clients by layering up bits of functionality (JSON encoding/decoding, Auth, URL rewriting, error code handling, etc). In particular it’s trivial to interact with an authless JSON API.

The idea was to create a generic idea of what a REST API is so that it’s possible to create variants of APIs - e.g. when the same API is hosted at a different URL or with a different authentication scheme.

It’s not on GitHub yet but I can add it later.

1 Like