简体   繁体   English

http在Node.js和Python Flask服务器之间发布了base64 json请求,但是Node收到了不完整的json

[英]http post a base64 json request between Node.js and Python Flask server but Node received an incomplete json

I try to send a base64 json form node.js server to python flask server and than return a same base64 code back to node.js server. 我尝试将base64 json形式的node.js服务器发送到python flask服务器,然后将相同的base64代码返回给node.js服务器。 Flask can successfully receive my json but when it response to node and node try to print out the response. Flask可以成功接收我的json,但是当它对node和node的响应时,尝试打印出响应。 I got a error message say: "Unexpected end of JSON input". 我收到一条错误消息:“ JSON输入意外结束”。 I found the reason is node server can not receive the base64 completely. 我发现原因是节点服务器无法完全接收到base64。 It just only receive a small portion. 它仅接收一小部分。 What is the problem? 问题是什么? Is post request has a string limit? 发布请求是否有字符串限制?

I tested when I change the base64 code to a short string. 我将base64代码更改为短字符串时进行了测试。 Node server can receive response normally. 节点服务器可以正常接收响应。

Anyone can help me? 有人可以帮助我吗? Thank you. 谢谢。

This is my code: 这是我的代码:

<<< Node.js Server >>> <<< Node.js服务器>>>

var express = require('express');
var http = require('http');

var app = express();

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(10000, () => console.log('Running on http://localhost:10000'));

postData = JSON.stringify({
    'code': <base64 code or short string here>
});

var options = {
    hostname: 'localhost',
    port: 10001,
    path: '/test',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(postData)
    }
};

var req = http.request(options, (res) => {
    res.on('data', (chunk) => {
        var data = JSON.parse(chunk);
        console.log(data.message);
    });
    res.on('end', () => {
        console.log('No more data in response.');
    });
});

req.on('error', (e) => {
    console.error(`problem with request: ${e.message}`);
});

req.write(postData);
req.end();

<<< Python Flask Server >>> <<< Python Flask服务器>>>

from flask import Flask
from flask import request
from flask import jsonify

app = Flask(__name__)

@app.route('/test', methods=['POST'])
def test():
    request_data = request.get_json()
    print(request_data['code'])
    return jsonify(
        message = request_data['code']
    )

app.run(host='localhost', port=10001)

In NodeJS code, in data event, you will get chunk data(partial data), you need to wait until end event and then parse, following example may help you 在NodeJS代码中,在data事件中,您将获取块数据(部分数据),需要等到end事件再解析,以下示例可能对您有所帮助

var req = http.request(options, (res) => {
    var data = '';
    res.on('data', (chunk) => {
        data += chunk.toString(); // buffer to string
    });
    res.on('end', () => {
        data = JSON.parse(data);
        console.log(data.message);
        console.log('No more data in response.');
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM