Git Product home page Git Product logo

Comments (9)

josevalim avatar josevalim commented on September 23, 2024

path is already as path_info (it returns a list though). You can easily convert it back to a path though. We won't have a body/1 but we will have built-in parameter parsing. I will add cookies (both request and response cookies) when I am done with parameter parsing. Thanks!

from plug.

0xAX avatar 0xAX commented on September 23, 2024

If request will have json body payload, how to get it?

from plug.

josevalim avatar josevalim commented on September 23, 2024

You just need to create a parser like this one:

https://github.com/elixir-lang/plug/blob/master/lib/plug/parsers/urlencoded.ex

I am working on the api for plugging different parsers (I will be done with it today) but you will need to do something like:

alias Plug.Parsers, as: P
P.call(conn, parsers: [P.URLENCODED, P.MULTIPART, P.MY_JSON_PARSER], limit: 8_000_000)

from plug.

josevalim avatar josevalim commented on September 23, 2024

Ok, the parsing API is done. You can use urlencoded above as an example for your own json parsing and calling parsing works as shown above. Tests can be found here (and you can use similar to test your own parsers):

https://github.com/elixir-lang/plug/blob/master/test/plug/parsers_test.exs

/cc @devinus

from plug.

josevalim avatar josevalim commented on September 23, 2024

Now I am starting work on cookies. :)

from plug.

glesica avatar glesica commented on September 23, 2024

Hijacking this instead of opening a new issue. It's not entirely clear to me how one would go about adding a new parser, as mentioned above. So, for instance, I want to implement a JSON parser. It seemed pretty simple:

defmodule Mineshaft.Parsers.JSON do
  @moduledoc false
  alias Plug.Conn

  def parse(Conn[] = conn, "application", "json", _headers, opts) do
    read_body(conn, Keyword.fetch!(opts, :limit))
  end

  def parse(conn, _type, _subtype, _headers, _opts) do
    { :next, conn }
  end

  defp read_body(Conn[adapter: { adapter, state }] = conn, limit) do
    case read_body({ :ok, "", state }, "", limit, adapter) do
      { :too_large, state } ->
        { :too_large, conn.adapter({ adapter, state }) }
      { :ok, body, state } ->
        { :ok, JSEX.decode(body), conn.adapter({ adapter, state }) }
    end
  end

  defp read_body({ :ok, buffer, state }, acc, limit, adapter) when limit >= 0,
    do: read_body(adapter.stream_req_body(state, 1_000_000), acc <> buffer, limit - byte_size(buffer), adapter)
  defp read_body({ :ok, _, state }, _acc, _limit, _adapter),
    do: { :too_large, state }

  defp read_body({ :done, state }, acc, limit, _adapter) when limit >= 0,
    do: { :ok, acc, state }
  defp read_body({ :done, state }, _acc, _limit, _adapter),
    do: { :too_large, state }
end

However this results in an error at runtime, specifically Plug.Parsers.UnsupportedMediaTypeError gets raised. I suspect I'm missing something, anyone have any thoughts? This might be a simple documentation issue (or user error on my part...).

from plug.

ericmj avatar ericmj commented on September 23, 2024

@glesica Here is an example of a working JSON parser:

https://github.com/ericmj/explex_web/blob/aa9e0e5fc1afeaf2769b0d6a43a3373f0e3e3700/lib/explex_web/util/json_decoder.ex
https://github.com/ericmj/explex_web/blob/aa9e0e5fc1afeaf2769b0d6a43a3373f0e3e3700/lib/explex_web/router/api.ex#L12

from plug.

josevalim avatar josevalim commented on September 23, 2024

@glesica after you implement the parser you need to hook it up, as @ericmj did in the second example, and you should be good to go. :)

from plug.

glesica avatar glesica commented on September 23, 2024

Copied the example above (changing the bits that were specific to Explex) but I still get the same error. I was not positive where to put the second part where the parser gets hooked up, so I stuck it in the sugar router module, which seemed to be the closest to the example. It runs, but it errors out in the same place it was erroring out before.

from plug.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.