Post files to API server using Gin in Golang



Post files to API server using Gin in Golang

Post files to API server using Gin in Golang

In this video, we will learn how to post files to an API server using Gin in Golang.
Gin is a framework written in Golang, to help you create rest API in Go

we have also added the code below that we have used throughout this video

#Golang #go #API #GinGonic #theexceptionhandler #coding #postfiles #APIserver

Upload and Download PDF file using Golang
Uploading Files to Golang Web Server – Golang Web
Golang Uploading storing file
Upload File in Golang REST API
Uploading a file with Go
Go Tutorial Basic | Golang
Learn Go Programming – Golang Tutorial for Beginners
Go Language Programming Practical Basics Tutorial
Golang for beginners
Golang Tutorials
Go programming Tutorial for Beginners
Web Development with Go
Golang Web Course
Go Language Programming Practical Basics Tutorial

///code
package main

import (
“mime/multipart”
“net/http”
“github.com/gin-gonic/gin”
)

type user struct {
Id int `uri:”id”`
Name string `form:”name”`
Email string `form:”email”`
Avatar *multipart.FileHeader `form:”avatar” binding:”required”`
}
func main() {
r := gin.Default()
r.PUT(“/user/:id”, func(c *gin.Context) {
var userObj user
if err := c.ShouldBind(&userObj); err != nil {
c.String(http.StatusBadRequest, “bad request”)
return
}

if err := c.ShouldBindUri(&userObj); err != nil {
c.String(http.StatusBadRequest, “bad request”)
return
}

err := c.SaveUploadedFile(userObj.Avatar, userObj.Avatar.Filename)
if err != nil {
c.String(http.StatusInternalServerError, “unknown error”)
return
}

c.JSON(http.StatusOK, gin.H{
“status”: “ok”,
“data”: userObj,
})
})
r.Static(“assets”, “./assets”)

r.Run(“localhost:8080”)
}

Comments are closed.