Hi All!
I am stuck with this bizarre phenomenon that the http.Error function does
not repond with the customizable text defined by the second argument.
Suppose a simple ill http server with this function that responds an error
as always:
------------------------------
func (con *Net) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Whatever! I return an error", 403)
return
//the original functional codes are below and they won't get executed now
......
}
==================
So it is easy to transform any of your functional go http code with the two
magic lines and test the functionality of http.Error.
And here is some javascript that visits a particular url where your
ServeHTTP serves with GET method:
-----------------------------------
async function get(url){
let re
try{
re=await fetch(url,{method: 'GET'})
}catch(err){
alert(''The url your provided is not reachable!")
return Promise.reject(1)
}
if (re.status != 200){
alert(`Server responded with an error:${re.status} ${re.statusText}`)
console.log(`Server responded with an error:${re.status}
${re.statusText}`)
return Promise.reject(re.status)
}else{ // irrelevant in this situation
return re.json()
}
}
function test_Error403(){
get('[where the golang ServeHTTP function serves]').
then((msg) => console.log(msg))}
=============================
Then you can link test_Error403() function with a button on an html file,
but be aware to change '[where the golang ServeHTTP function serves]' to
real url.
The bizarre situation is I always get alert or console log like:
Server responded with an error:403
while what I expect is:
Server responded with an error:403 Whatever! I return an error
So is there anything wrong with the syntax? How can I respond the client
with an arbitrary string as `statusText` ?
Thanks in advance!
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/CADEX6_VwACq%3DG9hciRr0c1LO3tVFtKs0ebMcvXxvjmdaMx%2Byfw%40mail.gmail.com.