2016-07-04 43 views
6

有一些使用Phoenix製作的API,API使用JSON。如何爲Phoenix Request強制執行JSON編碼?

但是,當您測試它並向curl發送JSON時,它會失敗,因爲Phoenix不會將請求解析爲JSON。您需要明確添加application/json表頭到curl。我想讓它更加強大,並告訴Phoenix始終將所有請求解析爲JSON。

有沒有辦法強制Phoenix將請求視爲JSON並將其解析爲JSON?

UPDATE

我試圖用插頭設置請求頭作爲@AbM建議的,在路由器以下代碼:

def set_format conn, format do Plug.Conn.put_private conn, :phoenix_format, format end 

def put_req_header conn, {key, value} do Plug.Conn.put_req_header conn, key, value end 

pipeline :api do 
    plug :put_req_header, {"accept", "application/json"} 
    plug :put_req_header, {"content-type", "application/json"} 
    plug :set_format, "json" 
    plug :accepts, ["json"] 
end 

請求已經由具有CURL

curl -X POST http://localhost:4000/api/upload -d '{"key": "value"}'      

連接看起來像:

%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{}, 
before_send: [#Function<1.93474994/1 in Plug.Logger.call/2>, 
    #Function<0.119481890/1 in Phoenix.LiveReloader.before_send_inject_reloader/1>], 
body_params: %{"{\"key\": \"value\"}" => nil}, 
cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, 
host: "localhost", method: "POST", owner: #PID<0.483.0>, 
params: %{"{\"key\": \"value\"}" => nil}, 
path_info: ["api", "upload"], peer: {{127, 0, 0, 1}, 58408}, 
port: 4000, 
private: %{App.Router => {[], %{}}, 
    :phoenix_action => :upload, 
    :phoenix_controller => ApiController, :phoenix_endpoint => App.Endpoint, 
    :phoenix_format => "json", :phoenix_layout => {LayoutView, :app}, 
    :phoenix_pipelines => [:api], 
    :phoenix_route => #Function<8.59735990/1 in App.Router.match_route/4>, 
    :phoenix_router => App.Router, :phoenix_view => ApiView, 
    :plug_session_fetch => #Function<1.89562996/1 in Plug.Session.fetch_session/1>}, 
query_params: %{}, 
query_string: "", remote_ip: {127, 0, 0, 1}, 
req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, 
req_headers: [{"user-agent", "curl/7.37.1"}, {"host", "localhost:4000"}, 
    {"accept", "application/json"}, {"content-length", "16"}, 
    {"content-type", "application/json"}], 
request_path: "/api/upload", resp_body: nil, resp_cookies: %{}, 
resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, 
    {"x-request-id", "xxx"}], scheme: :http, 
script_name: [], 
secret_key_base: "xxx", 
state: :unset, status: nil} 

它的作品,如果我將-H "Content-Type: application/json"參數添加到CURL,沒有它不起作用。

+0

會在['put_req_header(conn,「accept」,「application/json」)添加一個插件到你的路由器中, ](https://hexdocs.pm/plug/Plug.Conn.html#put_req_header/3)解決你的問題? – AbM

+0

不是,它不工作:( –

+0

你可以詳細說明一下,也許顯示你的router.ex – AbM

回答

1

如果有人會爲此谷歌,並希望實施這樣的行爲。

lib/%APP_NAME%/endpoint.ex

plug Plug.Head 

# add custom plug 
plug :set_format, "json" 

定義它:

defp set_format(conn, format) do 
    if Regex.match?(~r/^api.*/, conn.host) do 
     Plug.Conn.put_private conn, :phoenix_format, format 
    else 
     conn 
    end 
    end 

在這個例子中,我們有骯髒的黑客,這將加強JSON格式的子域api

它不建議去做那些但是由於Phoenix在任何時候都會執行HTML來修復這種荒謬的行爲你喜歡:Elixir.Phoenix.Router.NoRouteError爲pipeline :api顯示404.json,而不是404.html