5

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.

7
  • 2
    The http.Request.Body ReadCloser will return EOF immediately if it is empty. Try dumping it to make sure it really is filled with what you expect. Commented Jun 28, 2020 at 5:43
  • @Marc what exactly does dumping it mean? Is that like printing r.Body? Commented Jun 28, 2020 at 5:59
  • 4
    It means reading the body and printing it. Before trying to parse it, you should make sure you're receiving what you actually expect. Commented Jun 28, 2020 at 5:59
  • Your server code (golang) is totally fine. There seems to be a problem in javascript post object. Can you change it to { path: "example/path/to" } Commented Jun 28, 2020 at 7:54
  • 1
    Use httputil.DumpRequest to inspect the request. Commented Jun 28, 2020 at 8:11

1 Answer 1

5

So it would seem that the issue is that my front-end is calling the endpoint twice which is why the EOF error is occurring. I wrapped all my code of my code in

if r.Method == "POST" {
 ...
}

And the issue has been resolved.

I believe that the extra endpoint hit was a CORS pre-flight thing or something.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.