printfcoder / stack-rpc-tutorials

Stack-RPC 中文示例、教程、资料,源码解读
https://stacklabs.cn
Apache License 2.0
1.69k stars 302 forks source link

运行 examples/basic-practices/micro-api/rpc 中的例子报错 #205

Closed heyuanchao closed 4 years ago

heyuanchao commented 4 years ago

运行 https://github.com/micro-in-cn/tutorials/tree/master/examples/basic-practices/micro-api/rpc 中的例子报错

# curl -H 'Content-Type: application/json' -d '{"name": "小小先"}' "http://localhost:8080/example/call"
POST http://localhost:8080/example/call
Content-Type: application/json

{"name": "小小先"}
{"id":"go.micro.api","code":500,"detail":"error during request: unknown field \"name\" in go_api.Request","status":"Internal Server Error"}

我改了下代码:

if req.Method != "POST" {
        return errors.BadRequest("go.micro.api.example", "method: [%v], require post", req.Method)
    }
# curl -H 'Content-Type: application/json' -d '{}' http://localhost:8080/example/foo/bar
POST http://localhost:8080/example/foo/bar
Content-Type: application/json
{"id":"go.micro.api.example","code":400,"detail":"method: [], require post","status":"Bad Request"}
printfcoder commented 4 years ago

这个问题一般是go-micro版本号不一致,比较老,原型不一造成的。也可能是使用的micro api的方式不正确。

如果版本老,且自己生成的原型文件,请重新拉代码重新生成。如果不是,那么基本就是micro api使用不对。确定使用的是micro api --handler=rpc。我刚试了正常。

heyuanchao commented 4 years ago

谢谢舒先,该问题已经解决