GenieFramework / Genie.jl

🧞The highly productive Julia web framework
https://genieframework.com
MIT License
2.28k stars 192 forks source link

@params(:JSON_PAYLOAD) = nothing #150

Closed DdZ-Fred closed 5 years ago

DdZ-Fred commented 5 years ago

Hello!

@params(:JSON_PAYLOAD) doesn't seem to find my payload. I did this based on the #102 issue. Did I forget something ? I'm still trying to get used to this awesome project.

Here's below the POST route:

# MY IMPORTS
using Genie, HTTP
using Genie.Router, Genie.Requests, Genie.Renderer
import Base.convert

struct Board
  id:: String
  name:: String
end

# [BOARDS] POST
route("/boards", method = POST, named = :create_board_item) do
  # Retrieve payload
  message = @params(:JSON_PAYLOAD)
  @show message
  # (:message => Board("1", message["name"])) |> json!
  (:message => Board("1", message["name"])) |> json!
end

Here are the details of my request:

POST /boards HTTP/1.1
Host: 127.0.0.1:8000
Content-Type: application/json
User-Agent: PostmanRuntime/7.15.0
Accept: */*
Cache-Control: no-cache
Postman-Token: b871672e-fb96-4e63-a2f5-2f955ebe6982,28d751bb-47c1-4516-9191-256f52e4b1db
Host: 127.0.0.1:8000
accept-encoding: gzip, deflate
content-length: 19
Connection: keep-alive
cache-control: no-cache

{
    "name": "Bapo"
}

Here is my terminal output:

[ Info: Ready!
2019-07-12 20:22:41:DEBUG:Main: Web Server starting at http://127.0.0.1:8000
message = nothing
2019-07-12 20:22:53:ERROR:Main: ArgumentError: Dict(kv): kv needs to be an iterator of tuples or pairs
2019-07-12 20:22:53:ERROR:Main: /boards 500

2019-07-12 20:22:54:CRITICAL:Main: ArgumentError: Dict(kv): kv needs to be an iterator of tuples or pairs
Dict(::Nothing) at ./dict.jl:132
getindex(::Nothing, ::String) at /home/frey/.julia/packages/Genie/7LOKY/src/Router.jl:75
(::getfield(Main, Symbol("##9#10")))() at /home/frey/__DEV_PLAYGROUND__/swapcard-practices/trello-clone/server/julia-api/main.jl:46
#invokelatest#1 at ./essentials.jl:742 [inlined]
invokelatest at ./essentials.jl:741 [inlined]
match_routes(::HTTP.Messages.Request, ::HTTP.Messages.Response, ::Nothing, ::Genie.Router.Params{Any}) at /home/frey/.julia/packages/Genie/7LOKY/src/Router.jl:485
route_request(::HTTP.Messages.Request, ::HTTP.Messages.Response, ::Sockets.IPv4) at /home/frey/.julia/packages/Genie/7LOKY/src/Router.jl:100
handle_request(::HTTP.Messages.Request, ::HTTP.Messages.Response, ::Sockets.IPv4) at /home/frey/.julia/packages/Genie/7LOKY/src/AppServer.jl:94
handle_request(::HTTP.Messages.Request, ::HTTP.Messages.Response) at /home/frey/.julia/packages/Genie/7LOKY/src/AppServer.jl:93
#14 at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/Distributed/src/macros.jl:108 [inlined]
run_work_thunk(::getfield(Genie.AppServer, Symbol("##14#15")){HTTP.Messages.Request,HTTP.Messages.Response}, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/Distributed/src/process_messages.jl:56
#remotecall_fetch#148(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Function, ::Distributed.LocalProcess) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/Distributed/src/remotecall.jl:364
remotecall_fetch(::Function, ::Distributed.LocalProcess) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/Distributed/src/remotecall.jl:364
#remotecall_fetch#152(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Function, ::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/Distributed/src/remotecall.jl:406
remotecall_fetch(::Function, ::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/Distributed/src/remotecall.jl:406
setup_http_handler(::HTTP.Messages.Request, ::HTTP.Messages.Response) at /home/frey/.julia/packages/Genie/7LOKY/src/AppServer.jl:61
setup_http_handler at /home/frey/.julia/packages/Genie/7LOKY/src/AppServer.jl:60 [inlined]
#6 at /home/frey/.julia/packages/Genie/7LOKY/src/AppServer.jl:40 [inlined]
handle at /home/frey/.julia/packages/HTTP/wY7Vc/src/Handlers.jl:253 [inlined]
handle(::HTTP.Handlers.RequestHandlerFunction{getfield(Genie.AppServer, Symbol("##6#12"))}, ::HTTP.Streams.Stream{HTTP.Messages.Request,HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}}) at /home/frey/.julia/packages/HTTP/wY7Vc/src/Handlers.jl:276
#4 at /home/frey/.julia/packages/HTTP/wY7Vc/src/Handlers.jl:345 [inlined]
macro expansion at /home/frey/.julia/packages/HTTP/wY7Vc/src/Servers.jl:360 [inlined]
(::getfield(HTTP.Servers, Symbol("##13#14")){getfield(HTTP.Handlers, Symbol("##4#5")){HTTP.Handlers.RequestHandlerFunction{getfield(Genie.AppServer, Symbol("##6#12"))}},HTTP.ConnectionPool.Transaction{Sockets.TCPSocket},HTTP.Streams.Stream{HTTP.Messages.Request,HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}}})() at ./task.jl:259

My Config

Julia Version 1.1.1 Commit 55e36cc308 (2019-05-16 04:10 UTC) Platform Info: OS: Linux (x86_64-pc-linux-gnu) CPU: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

Status ~/.julia/environments/v1.1/Project.toml [c52e3926] Atom v0.8.7 [c43c736e] Genie v0.9.5 #master (https://github.com/genieframework/Genie.jl) [cd3eb016] HTTP v0.8.2 [682c06a0] JSON v0.20.0 [2535ab7d] JSON2 v0.3.1 [e5e0dc1b] Juno v0.7.0 [8d0e9305] Nuklear v0.1.1 [197b51f5] Poptart v0.1.3 #master (https://github.com/wookay/Poptart.jl) [de0858da] Printf

essenciary commented 5 years ago

@DdZ-Fred Thanks for reporting this - turned out Postman was sending content-type and Genie was expecting Content-Type. Since the spec requires header keys and values to be case insensitive, I updated Genie accordingly.

I pushed to master and bumped Genie version - so either update Genie or re-add from master.

DdZ-Fred commented 5 years ago

ohh ok, well done! this is something I've never noticed about Postman

essenciary commented 5 years ago

Yes, it would be nice to be consistent when it comes to the "Temporary Headers" but since the spec says they should be case insensitive, it shouldn't matter (it was the content-length that was confusing Genie).

image

I'm closing this issue.