I want to trim the common prefix of requestUrl in the middleware ,

if ok :=strings.HasPrefix(requestURI,"/api");ok { c.Request.URL.Path = strings.TrimPrefix(requestURI,"/api") c.Request.RequestURI = strings.TrimPrefix(requestURI,"/api") } but it don't work

Description

Screenshots

Comment From: dmarkham

Hello @NoGroceries, I would like to help you debug this but i will need a little more info.. 1. I would like you to share a full example of the code your using and how your adding it into the router. 1. Where Do you want it to see the change? In the logs, other middleware that runs after you, the handlerfunc, Static File serving, cookie path, etc. 1. Maybe you can explain your motivation and that will help me understand!

Comment From: NoGroceries

Hello@dmarkham Thank you for your reply Our team is developing a software. It has a gateway and multiple micro services。All requests go through gateway, the front end uses ngnix and the same URL prefix.but the gateway need to remove the same prefix,the rest request url is the real url

Comment From: dmarkham

Ok so the request getting to gin looks like GET /api/users/12345 you then want to trim off /api in middleware and then have it route from that point forward internally to /users/12345?

If I understand correctly i'm not sure you can do this. By time your running middleware I think the system has already picked a handlerFunc. Maybe someone else has some ideas.

Comment From: yyq2013

Is it what you want to do? like url rewrite? I just write one for to ignore contextpath, if it in url.

import (
    "github.com/gin-gonic/gin"
    "github.com/golang/glog"
    "net/http"
    "strings"
)

type SkipContextPathRouter struct {
    ContextPath string
    Start       int
    *gin.Engine
}

func (r *SkipContextPathRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    if strings.HasPrefix(req.RequestURI, r.ContextPath) {
        glog.Infof("request url:%s, will skip prefix:%s", req.RequestURI, r.ContextPath)
        // remove context path before gin router process
        uri := req.RequestURI[r.Start:]
        req.RequestURI = uri
        req.URL.Path = req.URL.Path[r.Start:]
    }
    r.Engine.ServeHTTP(w, req)
}

contextPathToSkip := "/api/v1/"
    skipContextPathRouter := &router.SkipContextPathRouter{
        ContextPath: contextPathToSkip,
        Start:       len(contextPathToSkip),
        Engine:      ginRouter,
    }
    s := &http.Server{
        Addr:           ":8080",
        Handler:        skipContextPathRouter,
        MaxHeaderBytes: 1 << 20,
    }

Comment From: hrabkin

It doesn't work for me as well,

func AppRewriter() gin.HandlerFunc {
    return func(c *gin.Context) {
        prefix := "/app"
        c.Request.URL.Path = strings.TrimPrefix(c.Request.URL.Path, prefix)
        c.Request.RequestURI = c.Request.URL.Path
        c.Next()
    }
}

router := gin.Default()
router.Use(AppRewriter())

router.POST("/update-option", updateOption)

I call 127.0.0.1/app/update-option but get 404