php 如何在http中使用x-protobuf作为传输协议?
在对接的第三方文档中有明确指明:
1. 通信协议为HTTP
2. 序列化协议使用protobuf3,
3. HTTP请求头Content-Type:application/x-protobuf。
于是按照文档的.proto文件生成php代码,中间参考了一些资料:https://blog.csdn.net/u012129...,最后自己进行序列化和反序列化都能没有问题。想着应该是问题不大,但是在实际的请求中对方返回错误:“I/O error while reading input message; nested exception is com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length.。 第一反应难道是我的使用方式不对?上代码:
//序列化
$request = new \Request();
$request->setOs(2);
$request->setDeviceId('869615046800475');
$request->setDeviceType(0);
$pack = $request->serializeToString();
//反序列化
$request = new \Request();
$request->mergeFromString($pack);
//var_dump($request->getOs(), $request->getDeviceType(), $request->getDeviceId());
//2, 0, 869615046800475
$options = [
'headers' => [
'Content-Type' => 'application/x-protobuf',
],
'json' => $pack
];
$response = $this->request(self::$api, [], 'POST', $options);
响应结果:
{"timestamp":"2021-07-12T06:09:08.739+0000","status":400,"error":"Bad Request","message":"I/O error while reading input message; nested exception is com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length.","path":"/api/rta/silent/common"}
请教下大佬们,我这么使用理解上有问题吗还是哪里不对?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论