Debugging History
[golang] grpc-gateway REST API Issue : last colon in URL occurs 'Not Found'
namho46
2020. 11. 12. 14:37
Problem Statement
- Last colon in URL path causes
404 Not Found
error
// Example request
GET http://example.com/api/v1/users/user:123
// Example response
404 Not Found
Environment
- grpc-gateway for REST API
- golang grpc server
Reason
If you generated grpc-gateway
using go:generate protoc
, go to generated pb.pw.go
file. Then you can find following function at pattern_
local variables.
var pattern_Service_Create_User0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v1", "users"}, "", runtime.AssumeColonVerbOpt(true)))
You can see description about runtime.AssumeColonVerbOpt(true)
by navigating. It says "AssumeColonVerbOpt indicates whether a path suffix after a final colon may only be interpreted as a verb.".
So if you want to use a path suffix after a final colon as a part of resource, you have to modify parameter as false
.
var pattern_Service_Create_User0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v1", "users"}, "", runtime.AssumeColonVerbOpt(false)))
Finally
// Example request
GET http://example.com/api/v1/users/user:123
// Example response
{
"resultCode": "SUCCESS",
"message": "success",
"user": {
"name": "NathanKim",
}
}