I keep getting an EOF error and I'm not sure why. I have used print statements to check and the method is POST and the content-type is application/json which is to be expected. The json I am expecting looks like this { "path": "example/path/to" } The code runs successfully, but prints out an error which is also weird because on an error it should return before finishing the rest of the function
func create(w http.ResponseWriter, r *http.Request) {
dump, er := httputil.DumpRequest(r, true)
if er != nil {
http.Error(w, fmt.Sprint(er), http.StatusInternalServerError)
return
}
fmt.Println(string(dump))
var req map[string]string
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&req)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(req["path"])
err = os.MkdirAll("/"+req["path"], os.ModePerm)
if err != nil {
fmt.Println(err)
return
}
}
It only prints out "Bob" and "EOF", and I've done some extensive googling but I really can't find this question answered anywhere else I have also tried using r.ParseForm and using r.FormValue and PostFormValue, but when I do that the string is empty.
Also in case its useful, here is the front end code of value
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ "path": "example/path/to" })
})
EDIT: This is my DumpRequest
Listening on :5000
OPTIONS /api/create HTTP/1.1
Host: localhost:5000
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Origin: http://localhost:3000
Referer: http://localhost:3000/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
EOF
POST /api/create HTTP/1.1
Host: localhost:5000
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 47
Content-Type: application/json
Origin: http://localhost:3000
Referer: http://localhost:3000/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
{"path":"/Users/paul/Desktop/temp/new/hi 2/hi"}
What's confusing to me is that the EOF error shows up in between the DumpRequest messages.