Comments (9)
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.
If request will have json body payload, how to get it?
from plug.
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.
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.
Now I am starting work on cookies. :)
from plug.
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.
@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.
@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.
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)
- Mismatch spec for `Plug.Conn.Query.decode` HOT 1
- Plug.RequestId: put the `request_id` in `:private` or `:assigns`? HOT 1
- 1.15 changes decoding behaviour of `[]` HOT 5
- Request: Support MFA tuple for Plug.Session opts HOT 4
- Plug.Upload's random-tempdir generation is not very random HOT 1
- `strip_spaces` fails on `nil` since 1.15.0 HOT 1
- Plug.Conn.Query has changed the way it decodes parameters from 1.14 to 1.15 without warning HOT 1
- More flexible dynamic routes with `:` parameters HOT 2
- Compatibility issue with plug_crypto 2.0.0 HOT 3
- Error: There isn't enough space to open the popup HOT 1
- Docs inconsistency between Plug moduledoc and Plug.run HOT 1
- Plug.Upload: demonitor previous owner when giving away HOT 1
- Compilation issues on Erlang 26.x HOT 3
- html array items are dropped. HOT 1
- Plug.Conn is not closed when user closes the browser's tab (SSE) HOT 2
- setting max_age to nil in put_resp_cookie/4 causes crash HOT 1
- Sending a zip created on-the-fly via Plug.Conn.chunk fails after around 200Mb HOT 8
- Performance Regression in Plug.Conn.Cookies.decode/1 after Upgrading Elixir and Erlang HOT 3
- Plug.Router macros and functional plugs HOT 2
- Wrong editor links when running inside a container HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from plug.