-1

This question already asked but it is not solve my issue.

In my Go project am not able to print path and filename. It is showing some error like below:

2021/10/13 16:25:07 http: panic serving [::1]:60170: runtime error: invalid memory address or nil pointer dereference goroutine 6 [running]:

My Postman collection enter image description here

my code

package main

import (
    "fmt"
    "net/http"

    "github.com/gorilla/mux"
)

func encodeFfmpeg(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "multipart/form-data")
    _, header, _ := r.FormFile("video")
    fmt.Println(header.Filename)
}

func main() {
    router := mux.NewRouter()

    router.HandleFunc("/encode", encodeFfmpeg).Methods("POST")

    // config port
    fmt.Printf("Starting server at 8080 \n")
    http.ListenAndServe(":8080", router)
}

Am trying to print filename with path eg: /home/ramesh/videos/video.mp4

kostix
  • 51,517
  • 14
  • 93
  • 176
Ramesh S
  • 585
  • 3
  • 10
  • 32
  • 2
    `FormFile` returns an error. Check the error *before* touching any of the other values returned by `FormFile`. If the error is not `nil` then `header` will most likely be `nil` and the expression `header.Filename` *will* panic. – mkopriva Oct 13 '21 at 11:08
  • 2
    Setting `w.Header().Set("Content-Type", "multipart/form-data")` will not modify `r` in any way. It makes no sense whatsoever to have it there unless you are also responding with a payload, i.e. writing a response body, of that specific content type. – mkopriva Oct 13 '21 at 11:09
  • @mkopriva: I added ` _, header, err := r.FormFile("video") if err != nil { panic(err) } ` it shows this error `http: panic serving [::1]:60476: no multipart boundary param in Content-Type goroutine 6 [running]:` – Ramesh S Oct 13 '21 at 11:20
  • 1
    That means that the `boundary` parameter in the `Content-Type` header of the Postman request is missing. This parameter is required for `multipart/form-data` to work properly. See this [answer](https://stackoverflow.com/questions/16015548/how-to-send-multipart-form-data-request-using-postman) (the boundary is mentioned at the bottom). – mkopriva Oct 13 '21 at 11:24
  • @mkopriva: I added the `boundary` like this `Content-Type:multipart/form-data;boundary=` please check this image https://ibb.co/GnhJMgj. But still showing same error – Ramesh S Oct 13 '21 at 11:49
  • 2
    The linked answer, its associated comments, and the other answers too, suggest that you should keep Content-Type unset, and leave it to Postman to set it automatically. [This](https://i.stack.imgur.com/vA0se.png) in the second answer has the heading *"Fails If In Header"*. [This](https://i.stack.imgur.com/08wZj.png) has the heading *"Works"*. – mkopriva Oct 13 '21 at 11:56
  • You are right now I removed `Content-Type` now it is working. Please add this as a answer I will give upvote and right tick – Ramesh S Oct 13 '21 at 11:58

1 Answers1

2

The sent request is missing the boundary parameter in the Content-Type header. This parameter is required for multipart/form-data to work properly.

In Postman remove the explicit Content-Type header setting and leave it to Postman to automatically set the header with the boundary parameter.

For more see: https://stackoverflow.com/a/16022213/965900 & https://stackoverflow.com/a/41435972/965900

Last but not least, do not ignore errors.

mkopriva
  • 35,176
  • 4
  • 57
  • 71