-
I think I need to set CORS headers on all responses, and not only pre-flight requests. I could of course have my main handlers return a What would be the best way to add headers using middleware? I see the pre-flight example in the docs, which is all nice, but perhaps a bit too complicated as an initial example. I attempted this (which seems to work): function addCORSheader(handler)
function(req::HTTP.Request)
function wrap(response::HTTP.Response)
push!(response.headers, CORS_HEADERS)
response
end
return wrap(handler(req))
end
end
@get "/dummy" function()
return HTTP.Response(200, CORS_HEADERS, "Test")
end
@get router("/dummy2", middleware=[addCORSheader]) function()
"Test"
end but it feels a bit convoluted. I believe a simple example in the documentation would help (at least it would have helped me). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @ahjulstad, In your specific use case, I'd recommend setting your middleware function at the application level. Any middleware defined at the application level intercepts all incoming requests and outgoing responses. serve(middleware=[addCORSheader]) |
Beta Was this translation helpful? Give feedback.
Hi @ahjulstad,
In your specific use case, I'd recommend setting your middleware function at the application level. Any middleware defined at the application level intercepts all incoming requests and outgoing responses.