Gin: [GIN-debug] redirecting request 307

Created on 11 Jul 2017  路  7Comments  路  Source: gin-gonic/gin

i didn't found any answer for this issue .
I am trying to call simple API, but it redirecting request 307 . and the request fail on client side .
as you see in the first screen shot there is 3 requests . first one is option with Status Code:200 OK .
the next 2 requests with Status Code:307 Temporary Redirect
port 3030 is server
port 8100 is client
CORS middleware is gin-cors

image

image

image

and this is the code

// Creates a gin router with default middleware:
    router := gin.Default()
    // Apply the middleware to the router (works with groups too)
    router.Use(cors.Middleware(cors.Config{
        Origins:        "*",
        Methods:        "GET, PUT, POST, DELETE",
        RequestHeaders: "Origin, Authorization, Content-Type",
        ExposedHeaders: "",
        MaxAge: 300 * time.Second,
        Credentials: false,
        ValidateHeaders: false,
    }))

       router.POST("/api/v1/login", usersController.Login)

client side error

XMLHttpRequest cannot load http://localhost:3030/api/v1/login/. Redirect from
 'http://localhost:3030/api/v1/login/' to 'http://localhost:3030/api/v1/login' has been blocked by CORS 
policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 
'http://localhost:8100' is therefore not allowed access.

it works fine on postman

Most helpful comment

i think , this is a bug, if your url is /login ,it well return http 200 ,but if /login/ it will return http 307.

All 7 comments

@almgwary I see two different hosts (port 8100 and 3030), also which cors middleware are you using?

port 3030 is server
port 8100 is client
CORS middleware is gin-cors

@almgwary In the 1st screenshot it shows that the allowed host is the one with port 8100, not the one you're making the request from.

This may be a bug or a misconfiguration from the middleware, so you should open an issue there (as I saw you did, https://github.com/itsjamie/gin-cors/issues/9).

You can also try a different cors middleware like github.com/gin-contrib/cors

closing as the issue is not related with gin itself

SOLVED

problem was with URL

  • ...url/login Passed
  • ...url/login/ Failed because of extra /

@almgwary good catch, check https://github.com/gin-gonic/gin/blob/master/gin.go#L58-L63 too

i think , this is a bug, if your url is /login ,it well return http 200 ,but if /login/ it will return http 307.

SOLVED

problem was with URL

* `...url/login ` **Passed**

* `...url/login/ ` **Failed** because of extra `/`

This worked for me

Was this page helpful?
0 / 5 - 0 ratings