gin - problem accessing url-encoded path-param containing forward slash
Asked Answered
S

1

5

For a given route with path param (example below)

router.GET("/employee/:id", empHandler.GetEmployee)

When tried to invoke the url with id path-param(encoded) containing forward slashes

id = 21/admin/527

url-encoded id = 21%2Fadmin%2F527

https://localhost:8000/emplayee/21%2Fadmin%2F527

I'm getting 404 when I try to hit this request It seems that gin is automatically decoding the path param and forming a route with url containing decoded path-param

https://localhost:8000/emplayee/21/admin/527

I want the exact encoded value for employee id path-param since it is to be used for calling other api which requires it to be url-encoded.

Selfsufficient answered 23/3, 2022 at 4:25 Comment(0)
S
7

I've resolved this issue by configuring the router with below options

router.UseRawPath = true
router.UnescapePathValues = false

This resolved the 404 error, also gin context return the same encoded(unescaped) value. This value can now be used to call the other APIs which requires url-encoded(unescaped) value for employee id

Selfsufficient answered 23/3, 2022 at 4:25 Comment(2)
I wanted to understand why both values are needed. The gin documentation says : "If UseRawPath is false (by default), the UnescapePathValues effectively is true, as url.Path gonna be used, which is already unescaped." So that is why both values need to be overridden.Sidwohl
Is there a similar setting in go-chi?Hammurabi

© 2022 - 2024 — McMap. All rights reserved.