Giter Site home page Giter Site logo

Comments (24)

taoso avatar taoso commented on May 7, 2024 1

你这个传错了吧。你这里定义的 id 类型是 int64,不是字符串。你可以从官方文档查询 json 和 protobuf 类型的对应关系。

你试试

curl http://localhost:8080/twirp/todo.v1.ToDoService/Read -X POST -H "Content-Type:application/json" -d '{"id":1,"api":"1"}'

from sniper.

taoso avatar taoso commented on May 7, 2024 1

起这么早,少见

from sniper.

andyjia avatar andyjia commented on May 7, 2024

@lvht 大佬请帮看下

from sniper.

taoso avatar taoso commented on May 7, 2024

这个 failed to write response 一般是客户端超时主动断开连接呀。把你的项目上传 github,我看一下。

from sniper.

andyjia avatar andyjia commented on May 7, 2024

https://github.com/andyjia/sniper

from sniper.

andyjia avatar andyjia commented on May 7, 2024

请求这个url
localhost:8080/twirp/todo.v1.ToDoService/Read

from sniper.

taoso avatar taoso commented on May 7, 2024

我本地试了一下,没有问题呀。你指定 CONF_PATH 环境变量了吗?

image

from sniper.

andyjia avatar andyjia commented on May 7, 2024

我没有指定CONF_PATH环境变量
image

from sniper.

taoso avatar taoso commented on May 7, 2024

@andyjia 我搞糊涂了。我自己本地设了 CONF_PATH,在你的项目下要重新设置的。sniper 默认为加载项目根目录下的 sniper.toml。
image

from sniper.

andyjia avatar andyjia commented on May 7, 2024

我连续多次的刷新post,有的时候timeout,有的时候显示出正确的结果。(设置CONF_PATH与否结果相同)
image

from sniper.

andyjia avatar andyjia commented on May 7, 2024

我用的postman

image

from sniper.

taoso avatar taoso commented on May 7, 2024

你的 postman 的请求超时时间是不是设太短了。你先用 curl 直接调试试。

from sniper.

andyjia avatar andyjia commented on May 7, 2024

curl结果也是有时正常,有时timeout
mac:~ andyjia$ curl http://localhost:8080/twirp/todo.v1.ToDoService/Read -X POST -d '{"id":1}'
timeoutmac:~ andyjia$
mac:~ andyjia$
mac:~ andyjia$ curl http://localhost:8080/twirp/todo.v1.ToDoService/Read -X POST -d '{"id":1}'
timeoutmac:~ andyjia$
mac:~ andyjia$
mac:~ andyjia$ curl http://localhost:8080/twirp/todo.v1.ToDoService/Read -X POST -d '{"id":1}'
{"api":"1","toDo":{"id":"1","title":"a","description":"b"}}mac:~ andyjia$

from sniper.

taoso avatar taoso commented on May 7, 2024

默认有个 600ms 的超时。是不是你的 db 很慢?配置里加个一行 OUTER_API_TIMEOUT=3s 试试。

image

from sniper.

taoso avatar taoso commented on May 7, 2024

还有就是你的代码要一路传递 ctx 对象

from sniper.

andyjia avatar andyjia commented on May 7, 2024

大佬真牛!这样设置就好了,我这个是remote的数据库。
OUTER_API_TIMEOUT="3s"

from sniper.

andyjia avatar andyjia commented on May 7, 2024

还有就是你的代码要一路传递 ctx 对象

是这样的操作么?(最开始这样搞的,出错了乱试着改了。context没理解透彻。)
func Read(id int) (*sql.Rows, error) {
ctx := context.Background()
dbc := db.Get(ctx, "default")
修改为
func Read(ctx context.Context, id int) (*sql.Rows, error) {
dbc := db.Get(ctx, "default")

from sniper.

taoso avatar taoso commented on May 7, 2024

对,ctx 用于超时控制,要一路传递的。

from sniper.

andyjia avatar andyjia commented on May 7, 2024

领悟了~谢谢指导~

from sniper.

andyjia avatar andyjia commented on May 7, 2024

@lvht
试了好久,不知道什么原因,下面代码段,post的时候传递的是1,而下面获取req的结果req.GetId()的值为0。请问您知道可能的原因么?万分感谢。

func (s *Server) Read(ctx context.Context, req *pb.ReadRequest) (*pb.ReadResponse, error) {
log.Get(ctx).Infof("id=%d", req.GetId())
result, err := todo.Read(ctx, int(req.GetId()))

from sniper.

andyjia avatar andyjia commented on May 7, 2024

解决了。我curl post方法的问题(json内容大小写区分)。抱歉打扰了。
memo下,用下面的方式OK。

  1. curl
    curl http://localhost:8080/twirp/todo.v1.ToDoService/Read -X POST -H "Content-Type:application/json" -d '{"id":"1","api":"1"}'
  2. postman
  • way 1
    raw - JSON(application/json)
    {
    "api":"1",
    "id":"1"
    }

  • way 2
    x-www-form-urlencoded
    id =1
    api=1

from sniper.

andyjia avatar andyjia commented on May 7, 2024

谢谢提供的链接,谢谢大佬指导!
框架写的真好!

from sniper.

andyjia avatar andyjia commented on May 7, 2024

下一步我引入jwt试试

from sniper.

taoso avatar taoso commented on May 7, 2024

写个 hook 就好了

from sniper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.