The file is changed in the process of saving to the server

Viewed 58

I work with a local server via Flask. The server must accept a POST request with a .log file and save it to itself. But the file is saved, changing.

curl request example

curl -d "@12333.log" -X POST http://127.0.0.1:5000/test/45322.log

python file

import os
from datetime import datetime
from flask import Flask, request

app = Flask(__name__)

SERVER_CONFIG = {'debug': True}
DATA_DIR = './data'


@app.route('/<uid>/<name>', methods=['POST'])
def main(uid, name):
    service = Service(uid)
    data = request.get_data()
    response = service.get_data(data, name)
    return response


class Service:
    def __init__(self, uid):
        self._make_dir(uid)
        self.uid = uid

    def get_data(self, request_body, name):
        timestamp = datetime.now()
        with open(f'{DATA_DIR}/{self.uid}/{timestamp}_{name}', 'wb') as fd:
            fd.write(request_body)
        return {'success': True, 'status': 'body saved'}

    def _make_dir(self, uid):
        if not os.path.exists(f'{DATA_DIR}/{uid}'):
            os.mkdir(f'{DATA_DIR}/{uid}')


if __name__ == '__main__':
    app.run(**SERVER_CONFIG)

The file being sent 12333.log

23.09.2021 06:24:41;23.09.2021 06:24:41;7;2100.000;2400.000;600.000;89912.000;85947.000;91562.000;229.000;229.000;226.000;0.999;0.999;0.999;2100.000;2400.000;600.000;90215.000;87121.000;92000.000;229.000;229.000;226.000;0.999;0.999;0.999;257400.000;3900.000;257400.000;0;0.000;0;0.000;35;0;
23.09.2021 07:25:00;23.09.2021 07:25:00;7;900.000;1500.000;600.000;80549.000;79915.000;93058.000;230.000;230.000;225.000;0.999;0.999;0.999;900.000;1500.000;600.000;81500.000;80519.000;93900.000;230.000;230.000;225.000;0.999;0.999;0.999;262200.000;4200.000;262200.000;0;0.000;0;0.000;43;0;
23.09.2021 08:25:03;23.09.2021 08:25:03;8;1800.000;900.000;600.000;87025.000;84954.000;89294.000;230.000;229.000;227.000;0.999;0.999;0.999;1800.000;900.000;600.000;87451.000;85121.000;90905.000;230.000;229.000;227.000;0.999;0.999;0.999;274200.000;3900.000;274500.000;0;0.000;0;0.000;33;0;
23.09.2021 09:25:16;23.09.2021 09:25:16;4;600.000;900.000;0.000;84913.047;80869.562;86413.047;229.000;231.000;226.000;0.999;0.999;0.999;600.000;900.000;0.000;87900.000;80700.000;85200.000;229.000;231.000;226.000;0.999;0.999;0.999;235500.000;3000.000;235800.000;0;0.000;0;0.000;18;0;

The file that is being saved

23.09.2021 06:24:41;23.09.2021 06:24:41;7;2100.000;2400.000;600.000;89912.000;85947.000;91562.000;229.000;229.000;226.000;0.999;0.999;0.999;2100.000;2400.000;600.000;90215.000;87121.000;92000.000;229.000;229.000;226.000;0.999;0.999;0.999;257400.000;3900.000;257400.000;0;0.000;0;0.000;35;0;23.09.2021 07:25:00;23.09.2021 07:25:00;7;900.000;1500.000;600.000;80549.000;79915.000;93058.000;230.000;230.000;225.000;0.999;0.999;0.999;900.000;1500.000;600.000;81500.000;80519.000;93900.000;230.000;230.000;225.000;0.999;0.999;0.999;262200.000;4200.000;262200.000;0;0.000;0;0.000;43;0;23.09.2021 08:25:03;23.09.2021 08:25:03;8;1800.000;900.000;600.000;87025.000;84954.000;89294.000;230.000;229.000;227.000;0.999;0.999;0.999;1800.000;900.000;600.000;87451.000;85121.000;90905.000;230.000;229.000;227.000;0.999;0.999;0.999;274200.000;3900.000;274500.000;0;0.000;0;0.000;33;0;23.09.2021 09:25:16;23.09.2021 09:25:16;4;600.000;900.000;0.000;84913.047;80869.562;86413.047;229.000;231.000;226.000;0.999;0.999;0.999;600.000;900.000;0.000;87900.000;80700.000;85200.000;229.000;231.000;226.000;0.999;0.999;0.999;235500.000;3000.000;235800.000;0;0.000;0;0.000;18;0;
1 Answers

Update your curl command. Replace -d to --data-binary:

curl --data-binary "@12333.log" -X POST http://127.0.0.1:5000/test/45322.log

The reason is that, when using -d or --data option to upload a file, curl command will remove carriage returns and newlines. --data-binary won't do that.

Related