Api Rest Error - Gin / [SOLVED]

Thanks to all, I know the forum is no place to discuss about CORS.
My real problem was just a port.
SOLVED.

  • I changed the port that the service listens to: 8080 and now I have a CORS error.
    Which I think I would avoid with this code
    Import “github.com/itsjamie/gin-cors” and then:

r.Use(cors.Middleware(cors.Config{
Origins: “http://localhost:3000”,
Methods: “GET, PUT, POST, DELETE”,
ExposedHeaders: “”,
Credentials: false,
ValidateHeaders: false,
}))

Thanks in advance.

Hi, I get 500 status code response from my localhost, posting from same localhost:3000
Both applications starts normaly, one is my web application that is more HTML than GO.
My IDE is JetBrains GogLand, a wonderful IDE with autocomplete, etc, etc… But, I think that import syntax -ViaApi/Model.go - so weird.

In the browser’s console:
XMLHttpRequest cannot load http://localhost:27275/api/user/. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:3000’ is therefore not allowed access. The response had HTTP status code 500.

I made same POST with POSTMAN but I get same error; then I try GET without parameter, same error. :frowning:

Can anyone help me, plz.
Thanks in advance and sorry for my english

Then, my code:

-------------MAIN.go
package main
import (
github.com/itsjamie/gin-cors
)
func main() {
r := RegisterApiRoutes()
r.Use(cors.Middleware(cors.Config{
Origins: “http://localhost:3000”,
Methods: “GET, PUT, POST, DELETE”,
ExposedHeaders: “”,
Credentials: false,
ValidateHeaders: false,
}))
r.Run(":27275")
}

---------CONTROLLER.go
package main
import (
github.com/gin-gonic/gin
“time”
“ViaApi/ACL”
“ViaApi/Model.go”
)
func RegisterApiRoutes() *gin.Engine {
r := gin.Default()
v1 := r.Group("/api/user")
{
v1.POST("/", PostUser)
v1.GET("/", GetUser)
v1.DELETE("/:id", DeleteUser)
v1.OPTIONS("/", Options)
}
return r
}
func Options(c *gin.Context){
}
func PostUser(c *gin.Context) {
var user Model.User
user.Name = c.PostForm(“Name”)
user.CellPhone = c.PostForm(“CellPhone”)
user.CreationDate = time.Now()
user.Street = c.PostForm(“Street”)
user.Number = c.PostForm(“Number”)
user.AddOn = c.PostForm(“AddOn”)
user.NeighborHood = c.PostForm(“NeighborHood”)
user.ZipCode = c.PostForm(“PostalCode”)
user.Email = c.PostForm(“Email”)
user.Password = c.PostForm(“Password”)
ACL.AddUser(user)
}
func GetUser(c *gin.Context) {
var user Model.User;
user.Name = "Alexandre"
c.JSON(200, user)
}

func DeleteUser(c *gin.Context){
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.