JuliaWebAPI.jl
JuliaWebAPI.jl copied to clipboard
JSON requests support
I'm trying to process POST requests with JSON body. For now I don't see a way how to do it.
parsepostdata doesn't recognize the header Content-Type: application/json and interprets the data as parameters of a query only.
Any suggestions why it is not implemented? Should I use other libraries for JSON processing like Mux.jl or is it just non implemented feature?
POST /document HTTP/1.1
Host: localhost
Content-Type: application/json
cache-control: no-cache
Postman-Token: 6761fa70-a0d8-413a-a31c-79e882cc17d2
{
"key":"document",
"value":{"text":"some text"},
"description":"",
"type":"text",
"enabled":true
}------WebKitFormBoundary7MA4YWxkTrZu0gW--
The primary purpose of this package is to enable exposing Julia functions as web APIs. Right now, the path elements are mapped to function parameters and query/form name-value pairs are mapped on to keyword args.
I unsure how requests with JSON body should fit to this paradigm. Mux does seem to be a good alternative for this.
Actually Web APIs primary means usage of some exchange format like JSON or XML. Just as example - https://market.mashape.com/ or open API initiative https://www.openapis.org/ But in both cases it requires usage of POST request with some HTTP body content.
In general it is not so big issue to add support of multipart HTTP content at least for JSON. But primary it is depends on how are you positioning the library.
- https://graphql.org as new conception of web API with JSON like input and output....
Implementation of JSON request processing with Mux:
using Mux
using JSON
@app test = (
Mux.defaults,
page(respond("<h1>Hello World!</h1>")),
page("/user/:user", req -> "<h1>Hello, $(req[:params][:user])!</h1>"),
route("/resource/process", req -> begin
obj = JSON.parse(String(req[:data]))
@show obj
Dict(:body => String(JSON.json(obj)),
:headers => Dict("Content-Type" => "application/json")
)
end),
Mux.notfound())
serve(test, 8080)
Base.JLOptions().isinteractive == 0 && wait()
Implementation of JSON request processing with Bukdu:
using Bukdu
using JSON
struct WelcomeController <: ApplicationController
conn::Conn
end
function index(c::WelcomeController)
render(JSON, "Hello World")
end
function process_resource(c::WelcomeController)
json = JSON.parse(String(c.conn.request.body))
@show json
render(JSON, json)
end
routes() do
get("/", WelcomeController, index)
post("/resource/process", WelcomeController, process_resource)
end
Bukdu.start(8080)
Base.JLOptions().isinteractive == 0 && wait()