Bad Request Handling
ueliwechsler opened this issue · 4 comments
ueliwechsler commented
How would I implement a 400 bad request
error handling in Bukdu
?
Should I overwrite the Bukdu.System.internal_error
method or is there a better way.
Thanks.
wookay commented
at first, see HTTP.jl
's implementation.
- https://github.com/JuliaWeb/HTTP.jl/blob/master/src/Servers.jl#L351
- https://github.com/JuliaWeb/HTTP.jl/blob/master/test/server.jl
do you have any other case?
using Bukdu
using Test
using Sockets
port = 8080
Bukdu.start(port)
# from https://github.com/JuliaWeb/HTTP.jl/blob/master/test/server.jl
# invalid HTTP
tcp = Sockets.connect(ip"127.0.0.1", port)
write(tcp, "GET / HTP/1.1\r\n\r\n")
sleep(0.1)
@test occursin(r"HTTP/1.1 400 Bad Request", String(readavailable(tcp)))
# no URL
tcp = Sockets.connect(ip"127.0.0.1", port)
write(tcp, "SOMEMETHOD HTTP/1.1\r\nContent-Length: 0\r\n\r\n")
sleep(0.1)
r = String(readavailable(tcp))
@test occursin(r"HTTP/1.1 400 Bad Request", r)
Bukdu.stop()
ueliwechsler commented
Thanks for your detailed response.
I am not sure, but I think my case is not covered.
In my application, I receive a lot of huge JSONs via Post request. I would like to check the content of the request if it contains the correct keys and return a Bad Request
Message if the keys does not exist.
wookay commented
then you could set the response.status
. make a function or plug.
function check_the_content(c::YourController)
# if the keys does not exist
c.conn.request.response.status = 400
end
ueliwechsler commented
Thank you, this works for me.