的NodeJS / EX preSS和POST二进制数据 [英] nodejs/express and binary data in POST

查看:2243
本文介绍了的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 4
BUF LEN 4
1 G.A
g.b 2
POST / API 200毫秒 - 23B

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

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


解决方案

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

例如:

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

同样重要的是,我需要设置多方的NodeJS解析器的编码为二进制。请参阅: https://github.com/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('http://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: https://github.com/superjoe30/node-multiparty/issues/37

这篇关于的NodeJS / EX preSS和POST二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆