Comments (24)
你这个传错了吧。你这里定义的 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.
起这么早,少见
from sniper.
@lvht 大佬请帮看下
from sniper.
这个 failed to write response
一般是客户端超时主动断开连接呀。把你的项目上传 github,我看一下。
from sniper.
https://github.com/andyjia/sniper
from sniper.
请求这个url
localhost:8080/twirp/todo.v1.ToDoService/Read
from sniper.
我本地试了一下,没有问题呀。你指定 CONF_PATH
环境变量了吗?
from sniper.
from sniper.
@andyjia 我搞糊涂了。我自己本地设了 CONF_PATH
,在你的项目下要重新设置的。sniper 默认为加载项目根目录下的 sniper.toml。
from sniper.
我连续多次的刷新post,有的时候timeout,有的时候显示出正确的结果。(设置CONF_PATH与否结果相同)
from sniper.
我用的postman
from sniper.
你的 postman 的请求超时时间是不是设太短了。你先用 curl 直接调试试。
from sniper.
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.
默认有个 600ms 的超时。是不是你的 db 很慢?配置里加个一行 OUTER_API_TIMEOUT=3s
试试。
from sniper.
还有就是你的代码要一路传递 ctx
对象
from sniper.
大佬真牛!这样设置就好了,我这个是remote的数据库。
OUTER_API_TIMEOUT="3s"
from sniper.
还有就是你的代码要一路传递
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.
对,ctx 用于超时控制,要一路传递的。
from sniper.
领悟了~谢谢指导~
from sniper.
@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.
解决了。我curl post方法的问题(json内容大小写区分)。抱歉打扰了。
memo下,用下面的方式OK。
- curl
curl http://localhost:8080/twirp/todo.v1.ToDoService/Read -X POST -H "Content-Type:application/json" -d '{"id":"1","api":"1"}' - postman
-
way 1
raw - JSON(application/json)
{
"api":"1",
"id":"1"
} -
way 2
x-www-form-urlencoded
id =1
api=1
from sniper.
谢谢提供的链接,谢谢大佬指导!
框架写的真好!
from sniper.
下一步我引入jwt试试
from sniper.
写个 hook 就好了
from sniper.
Related Issues (20)
- sniper中dao的model和proto自动生成的model如何映射? HOT 3
- 脚手架使用单独入口之后出现问题 HOT 7
- 实践中dao层如何复用? HOT 2
- 基础类型如何复用? HOT 1
- sniper 跟 kratos 的关系是? HOT 1
- Cannot use 'loginHooks' (type *"github.com/bilibili/twirp".ServerHooks) as type *"github.com/twitchtv/twirp".ServerHooks HOT 3
- 如何获取header头信息? HOT 2
- POST请求解决返回结果,int64类型的返回参数全部变为了string,期望:int64类型返回参数不要转为string HOT 1
- 如何实现文件上传? HOT 1
- service层的作用是什么,rpc参数是推荐传递到dao层吗? HOT 6
- 关于sniper的各层版本命名问题 HOT 2
- 如何更好的区分内外部服务? HOT 1
- 有完整的DEMO吗? HOT 1
- 【问题】为什么不在封装 db/mc/redis 支持,你们团队还在用这套 db/mc/redis? 不在支持后你们选择了那款db库 HOT 3
- readme 中的 '实现接口' 连接不对 HOT 2
- codahale/hdrhistogram repo url has been transferred under the github HdrHstogram umbrella HOT 3
- 微信讨论群
- 这里的 sniper 是不是应该用脚手架的传入的 pkg 参数替换一下?
- protoc-gen-twirp 链接问题 HOT 11
- 不清楚为什么install一直EOF HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sniper.