Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON requests support #73

Open
rssdev10 opened this issue Oct 21, 2018 · 4 comments
Open

JSON requests support #73

rssdev10 opened this issue Oct 21, 2018 · 4 comments

Comments

@rssdev10
Copy link
Contributor

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--
@tanmaykm
Copy link
Member

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.

@rssdev10
Copy link
Contributor Author

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.

@rssdev10
Copy link
Contributor Author

@rssdev10
Copy link
Contributor Author

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants