dongshan0202405 2017-08-27 03:18
浏览 1089

为什么Golang grpc-gateway的GET路由匹配POST请求?

I found a "get" route will match both HTTP GET and HTTP POST requests. For example:

rpc aaa(User) returns (User) {
  option (google.api.http) = {
  get: "/api/v1/aaa"
};

It matches both curl -v -X GET -k https://127.0.0.1/api/v1/aaa and curl -v -X POST -k https://127.0.0.1/api/v1/aaa.

I was wondering if it's possible to strictly match all routes including methods?

  • 写回答

1条回答 默认 最新

  • duanlu1922 2017-08-29 13:15
    关注

    Short answer

    It is because you did not specify the content-type of the request.

    Long answer

    By spec, it is allowed for you to encode your GET request into a POST method with Content-Type: application/x-www-url-encoded.

    So the request routing table in grpc-gateway tries to fallback from POST method to GET when the Content-Type of your request is application/x-www-url-encoded.

    ref. https://groups.google.com/d/msg/grpc-io/Xqx80hG0D44/1gwmwBcnNScJ

    "(Note) URL has a length limitation (go/longer-urls). It's enfoced by some browsers and proxies. If your GET request exceeds the limitation, browser may reject to send them. You may change to use a POST request with content type application/x-www-form-urlencoded instead. If the POST + URL is already been mapped to another backend function, you shall add "X-HTTP-Method-Override: GET" header to override the HTTP method and obtain the correct mapping.

    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划