Home > Blockchain >  How to get path and filename from postman request body using Go
How to get path and filename from postman request body using Go

Time:10-14

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

CodePudding user response:

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.

  • Related