iz4vve
iz4vve

Reputation: 41

Empty response after file upload

I have written a small REST api in Go and I am using the same functions to return a http.Response with a status code and a message:

type apiResponse struct {
    Status  int    `json:"status"`
    Message string `json:"message"`
}

I am marshalling this into a json string and using w.Write() to put it in the response.

The API has three endpoints, one of which lets the user upload a file. Two work perfectly fine and I'm getting the response I expect. The upload endpoint returns a valid response with a Content-Length that matches the message I expect, but when I read it (using ioutil.ReadAll), it's empty!

What am I doing wrong?

This is the function to read the Body:

func readResponseContent(resp *http.Response) string {
    defer resp.Body.Close()
    fmt.Println(resp)
    fmt.Println(resp.ContentLength)
    bodyBytes, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error in response: %s", err.Error())
        os.Exit(1)
    }
    bodyString := string(bodyBytes)
    return bodyString
}

and this is the handler:

func handleSubmission(w http.ResponseWriter, r *http.Request) {

var Buf bytes.Buffer
file, header, err := r.FormFile(audioUploadKey)

if err != nil {
    log.Printf("Error uploading file: %s\n", err.Error())
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
}
defer file.Close()

jobID, _ := uuid.NewUUID()
_ = os.MkdirAll(path.Join(jobsPath, jobID.String()), 0750)

log.Printf("Received file %s\n", header.Filename)

io.Copy(&Buf, file)
fileOut, _ := os.Create(path.Join(jobsPath, jobID.String(), 

Buf.WriteTo(fileOut)
Buf.Reset()

// submit
// DO STUFF with jobID

apiResp := apiResponse{Status:http.StatusCreated, Message:jobID.String()}
jsonResp, _ := json.Marshal(apiResp)
writeJSONResponse(w, jsonResp)
return}

Upvotes: 4

Views: 1260

Answers (2)

iz4vve
iz4vve

Reputation: 41

Found the problem... A snippet of code in a separate function was consuming the Body right before the response is generated...

Frustrations and mistakes of learning a new language :/

Upvotes: 0

John Balvin Arias
John Balvin Arias

Reputation: 2886

The problem looks like you are forgetting the content-type of the form you are receiving, when you send json the contentt-type is aplication/json, when you upload a file, you should be using multipart/form-data, if that´s you´re case you read it in this way:

    import(
       "ioutil"
       "net/http"
    )
//check all posible errors, I´m assuming you just have one file per key
    func handler(w http.ResponseWriter, r *http.Request) {
        r.ParseMultipartForm(1000000) //1 MB in memory, the rest in disk
        datas := r.MultipartForm
        for k, headers := range datas.File {
            auxiliar, _ := headers[0].Open() //first check len(headers) if it's correct
            fileName:=headers[0].Filename
            file, _ := ioutil.ReadAll(auxiliar)
            // do what you need to do with the file
    }
}
at the frontEnd you should have some javascript like this:

function handleFile(url,file){
  let data=new FormData();
  data.append("key",file); //this is the key when ranging over map at backEnd
  fetch(url,{method:"PUT",body:data})
}

Upvotes: 1

Related Questions