본문 바로가기
Debugging History

[golang] grpc-gateway REST API Issue : last colon in URL occurs 'Not Found'

by namho46 2020. 11. 12.
 

Problem Statement

  1. 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

  1. grpc-gateway for REST API
  2. 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",
    }
}

댓글