gpt4 book ai didi

elixir - HMAC、Elixir、Plug.Conn(尝试多次调用 read_body)

转载 作者:行者123 更新时间:2023-12-02 03:58:07 26 4
gpt4 key购买 nike

我正在努力解决一个问题,即在 Plug.Parsers.JSON 将其放入管道之前,某些东西正在读取 http 请求的正文。因此,json 插件中的 read_body 会超时——您无法读取正文两次。

我们在管道的早期插件中有一个 HMAC 实现,它在某些情况下读取主体。在 Plug 中, body 的使用行为是否有一个模式?我的意思是,如果我们只能读取一次,并且必须在 Plug.Parsers.JSON 中对其进行解码,那么......这是行不通的。

按照问题进行操作。生成 HMAC 哈希时是否需要包含请求正文?我的意思是,我觉得我们必须这样做,但此时我认为自己陷入了困境。

谢谢!

最佳答案

您可以将自定义 :body_reader 选项传递给 Plug.Parsers,以便缓存正文以供以后使用。

您不希望在解析器之前读取主体,而是缓存主体以便稍后从想要散列它的插件中读取。

Option :

:body_reader - an optional replacement (or wrapper) for Plug.Conn.read_body/2 to provide a function that gives access to the raw body before it is parsed and discarded. It is in the standard format of {Module, :function, [args]} (MFA) and defaults to {Plug.Conn, :read_body, []}.

Example :

Sometimes you may want to customize how a parser reads the body from the connection. For example, you may want to cache the body to perform verification later, such as HTTP Signature Verification. This can be achieved with a custom body reader that would read the body and store it in the connection, such as:

defmodule CacheBodyReader do
def read_body(conn, opts) do
{:ok, body, conn} = Plug.Conn.read_body(conn, opts)
conn = update_in(conn.assigns[:raw_body], &[body | (&1 || [])])
{:ok, body, conn}
end
end

which could then be set as:

plug Plug.Parsers,
parsers: [:urlencoded, :json],
pass: ["text/*"],
body_reader: {CacheBodyReader, :read_body, []},
json_decoder: Jason

已添加到 Plug v1.5.1 .

关于elixir - HMAC、Elixir、Plug.Conn(尝试多次调用 read_body),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51562448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com