http.ResponseWriter
için http durum kodunu nasıl belirlerim?Durum kodunu ayarlama on http.ResponseWriter
veya 403
Ben istekleri normalde bunlara bağlı 200 durum koduna sahip olduğunu görebilirsiniz.
http.ResponseWriter
için http durum kodunu nasıl belirlerim?Durum kodunu ayarlama on http.ResponseWriter
veya 403
Ben istekleri normalde bunlara bağlı 200 durum koduna sahip olduğunu görebilirsiniz.
http.ResponseWriter.WriteHeader
'u kullanın. Belge:
WriteHeader, durum kodu içeren bir HTTP yanıt üstbilgisi gönderir. WriteHeader açıkça çağrılmıyorsa, Write'a yapılan ilk çağrı örtülü bir WriteHeader'ı (http.StatusOK) tetikler. Böylece, WriteHeader'a yapılan açık çağrılar çoğunlukla hata kodlarını göndermek için kullanılır.
Örnek:
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Something bad happened!"))
}
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusForbidden)
tam listesini WriteHeader(int)
Apart here
örneğin, yardımcı yöntemi http.Error kullanabilirsiniz:
func yourFuncHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "my own error message", http.StatusForbidden)
// or using the default message error
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
http.Error() and http.StatusText() metodları arkadaşlarınız mı