How do I get Response statusCode in golang middleware?










0















How do I get Response statusCode in golang middleware?



ResponseWriter have only WriteHeader interface, I can't find get interface.










share|improve this question
























  • You just have a method like returnWith etc. that you use instead of using middleware

    – pale bone
    Nov 13 '18 at 1:46











  • so, it is can't get in golang middleware?

    – huangapple
    Nov 13 '18 at 2:09











  • Possible duplicate of stackoverflow.com/questions/35528330/…, stackoverflow.com/questions/42162211/… and others.

    – ThunderCat
    Nov 13 '18 at 2:17











  • I get, think you

    – huangapple
    Nov 13 '18 at 2:27











  • Possible duplicate of Gorilla mux, best way to 'catch' response codes

    – pale bone
    Nov 13 '18 at 2:31















0















How do I get Response statusCode in golang middleware?



ResponseWriter have only WriteHeader interface, I can't find get interface.










share|improve this question
























  • You just have a method like returnWith etc. that you use instead of using middleware

    – pale bone
    Nov 13 '18 at 1:46











  • so, it is can't get in golang middleware?

    – huangapple
    Nov 13 '18 at 2:09











  • Possible duplicate of stackoverflow.com/questions/35528330/…, stackoverflow.com/questions/42162211/… and others.

    – ThunderCat
    Nov 13 '18 at 2:17











  • I get, think you

    – huangapple
    Nov 13 '18 at 2:27











  • Possible duplicate of Gorilla mux, best way to 'catch' response codes

    – pale bone
    Nov 13 '18 at 2:31













0












0








0








How do I get Response statusCode in golang middleware?



ResponseWriter have only WriteHeader interface, I can't find get interface.










share|improve this question
















How do I get Response statusCode in golang middleware?



ResponseWriter have only WriteHeader interface, I can't find get interface.







go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 1:42









Pang

6,9011664101




6,9011664101










asked Nov 13 '18 at 1:39









huangapplehuangapple

1




1












  • You just have a method like returnWith etc. that you use instead of using middleware

    – pale bone
    Nov 13 '18 at 1:46











  • so, it is can't get in golang middleware?

    – huangapple
    Nov 13 '18 at 2:09











  • Possible duplicate of stackoverflow.com/questions/35528330/…, stackoverflow.com/questions/42162211/… and others.

    – ThunderCat
    Nov 13 '18 at 2:17











  • I get, think you

    – huangapple
    Nov 13 '18 at 2:27











  • Possible duplicate of Gorilla mux, best way to 'catch' response codes

    – pale bone
    Nov 13 '18 at 2:31

















  • You just have a method like returnWith etc. that you use instead of using middleware

    – pale bone
    Nov 13 '18 at 1:46











  • so, it is can't get in golang middleware?

    – huangapple
    Nov 13 '18 at 2:09











  • Possible duplicate of stackoverflow.com/questions/35528330/…, stackoverflow.com/questions/42162211/… and others.

    – ThunderCat
    Nov 13 '18 at 2:17











  • I get, think you

    – huangapple
    Nov 13 '18 at 2:27











  • Possible duplicate of Gorilla mux, best way to 'catch' response codes

    – pale bone
    Nov 13 '18 at 2:31
















You just have a method like returnWith etc. that you use instead of using middleware

– pale bone
Nov 13 '18 at 1:46





You just have a method like returnWith etc. that you use instead of using middleware

– pale bone
Nov 13 '18 at 1:46













so, it is can't get in golang middleware?

– huangapple
Nov 13 '18 at 2:09





so, it is can't get in golang middleware?

– huangapple
Nov 13 '18 at 2:09













Possible duplicate of stackoverflow.com/questions/35528330/…, stackoverflow.com/questions/42162211/… and others.

– ThunderCat
Nov 13 '18 at 2:17





Possible duplicate of stackoverflow.com/questions/35528330/…, stackoverflow.com/questions/42162211/… and others.

– ThunderCat
Nov 13 '18 at 2:17













I get, think you

– huangapple
Nov 13 '18 at 2:27





I get, think you

– huangapple
Nov 13 '18 at 2:27













Possible duplicate of Gorilla mux, best way to 'catch' response codes

– pale bone
Nov 13 '18 at 2:31





Possible duplicate of Gorilla mux, best way to 'catch' response codes

– pale bone
Nov 13 '18 at 2:31












1 Answer
1






active

oldest

votes


















0














This method is feasible.



 type loggingResponseWriter struct 
http.ResponseWriter
statusCode int


func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter
return &loggingResponseWriterw, http.StatusOK


func (lrw *loggingResponseWriter) WriteHeader(code int)
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)


func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request)
log.Printf("--> %s %s", req.Method, req.URL.Path)

lrw := NewLoggingResponseWriter(w)
wrappedHandler.ServeHTTP(lrw, req)

statusCode := lrw.statusCode
log.Printf("<-- %d %s", statusCode, http.StatusText(statusCode))
)






share|improve this answer























  • This method does have some issues though. It will break use of all the extra interfaces - Flusher, Pusher, CloseNotifier, and Hijacker.

    – Adrian
    Nov 13 '18 at 14:50











  • Yes, do you have any suggestions?

    – huangapple
    Nov 14 '18 at 16:10











  • This is the easiest/simplest method, just should be noted it breaks all the other interfaces. If those are needed as is status code logging, there are other options, like writing a func WriteHeader(w http.ResponseWriter, status int) that will call w.WriteHeader(status) and do the logging. But that requires making sure none of your code calls w.WriteHeader directly and uses the utility func instead. Have to weigh the pros/cons in each use case.

    – Adrian
    Nov 14 '18 at 16:15










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272536%2fhow-do-i-get-response-statuscode-in-golang-middleware%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This method is feasible.



 type loggingResponseWriter struct 
http.ResponseWriter
statusCode int


func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter
return &loggingResponseWriterw, http.StatusOK


func (lrw *loggingResponseWriter) WriteHeader(code int)
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)


func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request)
log.Printf("--> %s %s", req.Method, req.URL.Path)

lrw := NewLoggingResponseWriter(w)
wrappedHandler.ServeHTTP(lrw, req)

statusCode := lrw.statusCode
log.Printf("<-- %d %s", statusCode, http.StatusText(statusCode))
)






share|improve this answer























  • This method does have some issues though. It will break use of all the extra interfaces - Flusher, Pusher, CloseNotifier, and Hijacker.

    – Adrian
    Nov 13 '18 at 14:50











  • Yes, do you have any suggestions?

    – huangapple
    Nov 14 '18 at 16:10











  • This is the easiest/simplest method, just should be noted it breaks all the other interfaces. If those are needed as is status code logging, there are other options, like writing a func WriteHeader(w http.ResponseWriter, status int) that will call w.WriteHeader(status) and do the logging. But that requires making sure none of your code calls w.WriteHeader directly and uses the utility func instead. Have to weigh the pros/cons in each use case.

    – Adrian
    Nov 14 '18 at 16:15















0














This method is feasible.



 type loggingResponseWriter struct 
http.ResponseWriter
statusCode int


func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter
return &loggingResponseWriterw, http.StatusOK


func (lrw *loggingResponseWriter) WriteHeader(code int)
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)


func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request)
log.Printf("--> %s %s", req.Method, req.URL.Path)

lrw := NewLoggingResponseWriter(w)
wrappedHandler.ServeHTTP(lrw, req)

statusCode := lrw.statusCode
log.Printf("<-- %d %s", statusCode, http.StatusText(statusCode))
)






share|improve this answer























  • This method does have some issues though. It will break use of all the extra interfaces - Flusher, Pusher, CloseNotifier, and Hijacker.

    – Adrian
    Nov 13 '18 at 14:50











  • Yes, do you have any suggestions?

    – huangapple
    Nov 14 '18 at 16:10











  • This is the easiest/simplest method, just should be noted it breaks all the other interfaces. If those are needed as is status code logging, there are other options, like writing a func WriteHeader(w http.ResponseWriter, status int) that will call w.WriteHeader(status) and do the logging. But that requires making sure none of your code calls w.WriteHeader directly and uses the utility func instead. Have to weigh the pros/cons in each use case.

    – Adrian
    Nov 14 '18 at 16:15













0












0








0







This method is feasible.



 type loggingResponseWriter struct 
http.ResponseWriter
statusCode int


func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter
return &loggingResponseWriterw, http.StatusOK


func (lrw *loggingResponseWriter) WriteHeader(code int)
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)


func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request)
log.Printf("--> %s %s", req.Method, req.URL.Path)

lrw := NewLoggingResponseWriter(w)
wrappedHandler.ServeHTTP(lrw, req)

statusCode := lrw.statusCode
log.Printf("<-- %d %s", statusCode, http.StatusText(statusCode))
)






share|improve this answer













This method is feasible.



 type loggingResponseWriter struct 
http.ResponseWriter
statusCode int


func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter
return &loggingResponseWriterw, http.StatusOK


func (lrw *loggingResponseWriter) WriteHeader(code int)
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)


func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request)
log.Printf("--> %s %s", req.Method, req.URL.Path)

lrw := NewLoggingResponseWriter(w)
wrappedHandler.ServeHTTP(lrw, req)

statusCode := lrw.statusCode
log.Printf("<-- %d %s", statusCode, http.StatusText(statusCode))
)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 2:31









huangapplehuangapple

1




1












  • This method does have some issues though. It will break use of all the extra interfaces - Flusher, Pusher, CloseNotifier, and Hijacker.

    – Adrian
    Nov 13 '18 at 14:50











  • Yes, do you have any suggestions?

    – huangapple
    Nov 14 '18 at 16:10











  • This is the easiest/simplest method, just should be noted it breaks all the other interfaces. If those are needed as is status code logging, there are other options, like writing a func WriteHeader(w http.ResponseWriter, status int) that will call w.WriteHeader(status) and do the logging. But that requires making sure none of your code calls w.WriteHeader directly and uses the utility func instead. Have to weigh the pros/cons in each use case.

    – Adrian
    Nov 14 '18 at 16:15

















  • This method does have some issues though. It will break use of all the extra interfaces - Flusher, Pusher, CloseNotifier, and Hijacker.

    – Adrian
    Nov 13 '18 at 14:50











  • Yes, do you have any suggestions?

    – huangapple
    Nov 14 '18 at 16:10











  • This is the easiest/simplest method, just should be noted it breaks all the other interfaces. If those are needed as is status code logging, there are other options, like writing a func WriteHeader(w http.ResponseWriter, status int) that will call w.WriteHeader(status) and do the logging. But that requires making sure none of your code calls w.WriteHeader directly and uses the utility func instead. Have to weigh the pros/cons in each use case.

    – Adrian
    Nov 14 '18 at 16:15
















This method does have some issues though. It will break use of all the extra interfaces - Flusher, Pusher, CloseNotifier, and Hijacker.

– Adrian
Nov 13 '18 at 14:50





This method does have some issues though. It will break use of all the extra interfaces - Flusher, Pusher, CloseNotifier, and Hijacker.

– Adrian
Nov 13 '18 at 14:50













Yes, do you have any suggestions?

– huangapple
Nov 14 '18 at 16:10





Yes, do you have any suggestions?

– huangapple
Nov 14 '18 at 16:10













This is the easiest/simplest method, just should be noted it breaks all the other interfaces. If those are needed as is status code logging, there are other options, like writing a func WriteHeader(w http.ResponseWriter, status int) that will call w.WriteHeader(status) and do the logging. But that requires making sure none of your code calls w.WriteHeader directly and uses the utility func instead. Have to weigh the pros/cons in each use case.

– Adrian
Nov 14 '18 at 16:15





This is the easiest/simplest method, just should be noted it breaks all the other interfaces. If those are needed as is status code logging, there are other options, like writing a func WriteHeader(w http.ResponseWriter, status int) that will call w.WriteHeader(status) and do the logging. But that requires making sure none of your code calls w.WriteHeader directly and uses the utility func instead. Have to weigh the pros/cons in each use case.

– Adrian
Nov 14 '18 at 16:15

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272536%2fhow-do-i-get-response-statuscode-in-golang-middleware%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20