# JSON3.jl layer for HTTP.jl

**URL:** <https://discourse.julialang.org/t/json3-jl-layer-for-http-jl/100678>\
**Category:** Web Stack\
**Created:** [June 22, 2023, 12:40am UTC](https://discourse.julialang.org/t/json3-jl-layer-for-http-jl/100678 "2023-06-22T00:40:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 22, 2023, 12:40am UTC](https://discourse.julialang.org/t/json3-jl-layer-for-http-jl/100678/1 "2023-06-22T00:40:54Z")

</div>

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
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)
               end
           end
           function HTTP.Messages.bodysummary(obj::Union{JSON3.Object, JSON3.Array})
               io = IOBuffer()
               print(io, obj)
               String(take!(io))
           end
            
           HTTP.@client [json3_layer]
       end

```

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

---

<div class="post-metadata">

**Author:** ![AMJ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amj/32/214096_2.png) [@AMJ](https://discourse.julialang.org/u/AMJ)\
**Post date:** [June 22, 2023, 6:40am UTC](https://discourse.julialang.org/t/json3-jl-layer-for-http-jl/100678/2 "2023-06-22T06:40:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [June 22, 2023, 7:31am UTC](https://discourse.julialang.org/t/json3-jl-layer-for-http-jl/100678/3 "2023-06-22T07:31:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [June 22, 2023, 7:52am UTC](https://discourse.julialang.org/t/json3-jl-layer-for-http-jl/100678/4 "2023-06-22T07:52:19Z")

</div>

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.
