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

Add support for parsing JSON request body

Closed
xetorthio opened this issue Mar 25, 2014 · 10 comments
Closed

Add support for parsing JSON request body #129

xetorthio opened this issue Mar 25, 2014 · 10 comments

Comments

@xetorthio
Copy link

xetorthio commented Mar 25, 2014

Right now it's super easy and convenient to send a json response:

render.json(response).toFuture

We need something similar to parse json from the request body.

@twoism
Copy link
Contributor

twoism commented Mar 26, 2014

I like this idea. I've thought about it before. It seems like this would be best handled with a filter that looks at the request's Content-Type header and parses accordingly. Although, if the parsed json val was lazy you could do this with very little overhead and just add it to the Request class.

Maybe something like this?

class Request(val request: FinagleRequest) extends RequestProxy
  lazy val json: Option[WhatEverJacksonParsesInto] = request.headers.get("Content-Type") match  {
    case Some(header) if header == "application/json" =>
      // attempt to parse the contents of the request body
    case _ => None
  }
}

// Some controller
get("/json") { request => 
  request.json.match {
    case Some(json) => 
      render.plain(json.get("some_key").asString)
    case _ => render.plain("no json body found")
  }
}

Sound reasonable?

@adaptorel
Copy link

adaptorel commented Apr 23, 2014

Why option ? If you're accessing the request.json property you're obviously expecting a JSON request body so just return the appropriate BadRequest to the client. Even though I don't see much use for it, maybe a optJson alternative that will let you deal with the case the request is not JSON as you wrote above. In any case you might also need a higher level JSON manipulation library such as play-json (last time I checked it was usable outside play's scope) OR annotate the json method and force jackson into marshaling to that type and return it (ugly, don't like it. I would rather manipulate JSON).

@jfrazee
Copy link

jfrazee commented Jul 30, 2014

I think one of the virtues of finatra is that it's not especially opinionated about JSON handling. This is one of the primary reasons I ended up using it, because it made it easy to use whatever library I wanted (in my case Argonaut).

It would be a bad thing in my opinion to put this on Request, forcing one or the other of the several scala JSON libraries on the user because it in turn forces a particular regime for validation and parsing.

@nykolaslima
Copy link
Contributor

nykolaslima commented Apr 1, 2015

Hello guys,

Do we have a easy way to parse JSON from request body nowadays?

@scosenza
Copy link
Contributor

scosenza commented Apr 2, 2015

We are in the final stages of open-sourcing Finatra v2 which has robust support for parsing and validating JSON.

@nykolaslima
Copy link
Contributor

nykolaslima commented Apr 2, 2015

Hi @scosenza. Thanks for your response.

The development of Finatra ins't open source? Do you guys have a date for it?

Thanks again 😄

@caniszczyk
Copy link
Contributor

caniszczyk commented Apr 2, 2015

Technically it is open but we've been working on a bit of rewrite internally.

We plan on sharing it soon for community input before doing a 2.0

@nykolaslima
Copy link
Contributor

nykolaslima commented Apr 2, 2015

@caniszczyk do you guys have any planned date for it?

Can community help on this?

@cacoco
Copy link
Member

cacoco commented May 1, 2015

@nykolaslima Artifacts for Finatra (2.0.0.M1 -- the first/latest 2.0.0 milestone release) have been published to Maven Central and the 2.0.0.M1 branch merged into master. Feedback welcome!

@cacoco
Copy link
Member

cacoco commented May 12, 2015

JSON request body parsing is a feature in version 2.0.0.x, see README.md for more information on usage.

@cacoco cacoco closed this as completed May 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

8 participants