的NodeJS / EX preSS和POST二进制数据

编程入门 行业动态 更新时间:2024-10-25 00:30:11
本文介绍了的NodeJS / EX preSS和POST二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想二进制数据发送到一个前preSS应用程序。它工作正常,只要我的值小于0x80的小。如果一个值是0x80的或更大,它搅乱了整个缓冲区。

防爆preSS处理程序:

=二进制需要('二进制');exports.api =功能(REQ,RES){    VAR体= req.body.name;    VAR BUF =新的缓冲区(身体,'二进制');    的console.log(身体,req.body);    的console.log('REQ身体LEN,body.length);    的console.log('BUF LEN,buf.length);    变种G = binary.parse(BUF)        .word16bu('A')// 16位无符号大端值        .word16bu(B)。瓦尔    的console.log('G.A',G.A);    的console.log('g.b',g.b);  res.send(与资源应对);};

Python客户端(内容类型:应用程序/ x-WWW的形式urlen codeD):

进口要求从进口结构包#发送两个符号短裤(每个16位)。requests.post(的http://本地主机:3000 / API,数据= {'名':'HH'包(1,2)})

防爆preSS输出,当数据= 1,2。 这是我期望什么

{体名称:\\ u0000的\\ U0001 \\ u0000的\\ u0002'}REQ体LEN 4BUF LEN 41 G.Ag.b 2POST / API 200毫秒 - 23B

防爆preSS输出,当数据= 1,0xFF。这是有趣的是,9520实际上0x25的0x30十六进制,相当于0%的ASCII是。是的,这似乎是在解析%00%01 ...字符串。 我希望我能知道如何prevent这个!

{体名称:%00%01%00%FF'}REQ体len个12BUF LEN 129520 G.Ag.b 12325POST / API 200 2MS - 23B

解决方案

这竟然是一个编码问题。一切似乎默认为'UTF8',但在这种情况下,需要将其设置为'二进制

例如:

VAR BUF =新的缓冲区(body.toString('二进制'),'二进制');

同样重要的是,我需要设置多方的NodeJS解析器的编码为二进制。请参阅: github/superjoe30/node-multiparty/issues/37

I'm trying to send binary data to an express app. It works fine, as long as my values are smaller than 0x80. If a single value is 0x80 or greater, it messes up the whole buffer.

Express Handler:

binary = require('binary'); exports.api = function(req, res){ var body = req.body.name; var buf = new Buffer(body,'binary'); console.log('body',req.body); console.log('req body len', body.length); console.log('buf len', buf.length); var g = binary.parse(buf) .word16bu('a') // unsigned 16-bit big-endian value .word16bu('b').vars console.log('g.a', g.a); console.log('g.b', g.b); res.send("respond with a resource"); };

Python client (Content-Type: application/x-www-form-urlencoded):

import requests from struct import pack # send two unsigned shorts (16-bits each). requests.post('localhost:3000/api', data={'name':pack('!HH',1,2)})

Express output when data = 1,2. This is what I'd expect.

body { name: '\u0000\u0001\u0000\u0002' } req body len 4 buf len 4 g.a 1 g.b 2 POST /api 200 1ms - 23b

Express output when data = 1,0xFF. It's interesting to note that 9520 is actually 0x25 0x30 in hex, which corresponds to "%0" in ASCII. Yes, it appears to be parsing the '%00%01...' string. I wish I knew how to prevent this!!!

body { name: '%00%01%00%FF' } req body len 12 buf len 12 g.a 9520 g.b 12325 POST /api 200 2ms - 23b

解决方案

It turned out to be an encoding issue. Everything seems to default to 'utf8', but in this case it needs to be set to 'binary'.

For example:

var buf = new Buffer(body.toString('binary'),'binary');

Equally as important, I needed to set nodejs multiparty parser's encoding to 'binary'. See: github/superjoe30/node-multiparty/issues/37

更多推荐

的NodeJS / EX preSS和POST二进制数据

本文发布于:2023-10-09 18:09:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476415.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:二进制数   NodeJS   preSS   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!