Not able to modify response header
tttuuu888 opened this issue · 7 comments
First, thank you for this great package.
I've been looking for a way to add a custom header to the response but I couldn't find it.
(Specifically, I want to add Access-Control-Allow-Origin: *
to enable CORS)
I've also checked source but I couldn't find a way to modify header from outside of hunchentoot package, as far as I understood.
So I hope this is supported by hunchentoot. (If it's not really available at the moment)
Please let me know if there is anything I've missed.
Thanks.
Thanks for quick feedback.
I tried to make hunchentoot:easy-acceptor instance with custom-reply which including (:access-control-allow-origin . "*")
as below:
(setf *server*
(make-instance 'hunchentoot:easy-acceptor
:port 4242
:reply-class *custom-reply*))
And I made *custom-reply*
as below:
(defvar *custom-reply*)
(setf *custom-reply* (make-instance 'hunchentoot:reply))
(setf (hunchentoot:headers-out *custom-reply*)
(append (hunchentoot:headers-out *custom-reply*)
'((:access-control-allow-origin . "*"))))
But above process didn't work but only caused an error. Is this wrong approach?
I've opened the issue actually because I couldn't modify a reply instance using header-out function.
I would appreciate it if you let me know if I'm approaching the wrong way and giving me a little hint of a proper way.
I apologize if it's just a lack of my understanding, not an issue at all.
Thanks.
@stassats Modifying custom-reply with headers-out
didn't work because headers-out
was not an accesor
but a reader
on reply.lisp
, although it's an accesor on document. This was one of reasons that I thought this is an issue.
@stassats I'm sorry I just figured out there were header-out
and headers-out
and only headers-out
was a reader
.
@stassats I managed to make the *custom-reply*
as below:
(setf (hunchentoot:header-out :headers *custom-reply*)
(append (hunchentoot:header-out :headers *custom-reply*)
'((:access-control-allow-origin . "*"))))
But above code still didn't work.
I thought it's because of
Line 456 in f18c0aa
I thought it should be changed as below:
(*reply* (if (symbolp (acceptor-reply-class *acceptor*))
(make-instance (acceptor-reply-class *acceptor*))
(acceptor-reply-class *acceptor*)))
for the case that acceptor
is provided in a way of :reply-class *custom-reply*
.
Again, I would appreciate for any hint, sorry for the case of misunderstanding.
If I find out what I've done wrong, I'll leave it here for those who face similar problems like me .
Thanks.
You need to set your header-out in the handler.
@stassats Thanks a lot.
Now I can see Access-Control-Allow-Origin: *
is added in response header with below code:
(hunchentoot:define-easy-handler (test :uri "/test") (word)
(setf (hunchentoot:content-type*) "text/plain")
(setf (hunchentoot:header-out :Access-Control-Allow-Origin hunchentoot:*reply*) "*")
(format nil "word: ~a" word))
Thanks!