C'è qualche API fatta con Phoenix, l'API funziona con JSON.Come applicare la codifica JSON per Phoenix Request?
Tuttavia, quando si esegue il test e inviato JSON con curl
, l'operazione non riesce perché Phoenix non analizza la richiesta come JSON. È necessario aggiungere esplicitamente l'intestazione application/json
a curl
. Mi piacerebbe renderlo più robusto e dire a Phoenix di analizzare sempre tutte le richieste come JSON.
C'è un modo per forzare Phoenix a trattare sempre le richieste come JSON e analizzarle come JSON?
UPDATE
ho cercato di utilizzare la spina per impostare intestazioni di richiesta come suggerito @AbM, con il seguente codice nel router:
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
La richiesta è stata fatta con CURL
curl -X POST http://localhost:4000/api/upload -d '{"key": "value"}'
La connessione è simile a:
%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}
Funziona se aggiungo il parametro -H "Content-Type: application/json"
a CURL, senza di esso non funziona.
Sarebbe l'aggiunta di una spina nel vostro router con [ 'put_req_header (conn, "accetta", "application/json")' ] (https://hexdocs.pm/plug/Plug.Conn.html#put_req_header/3) risolve il problema? – AbM
No, non funziona :( –
Puoi approfondire un po 'di più Forse mostrare il tuo router.ex – AbM