Search code examples
pythonhttppostpython-requestscontent-type

multi-line payload is treated as single line in python request


I am simulating a post request where user sends multiline body data in post request. as per expectation this should be treated as multi line but on the application side this is treated as single line .

below payload is read from csv file using

with open(f"test_file.csv") as r:
        payload = r.read()

data in test_file.csv is -->

CO000PMAT;AFACT000PMAT;22/03/2022;1;PNRTE;;;EUR;;Reason
CO001PMAT;AFACT001PMAT;22/03/2022;2;PNRTE;;;EUR;;Reason

python request:

response = requests.post('https://example.com/api/v1/data?fileName=test_file.txt', data = payload, headers= {'Authorization':'XXXXX', 'Content-Type' : 'text/plain;charset=utf-8'}) 

in my expectation payload should be treated as 2 lines but in reality application is treating this as a single line .

could anybody suggest something ?


Solution

  • My problem is resolved by opening file in 'rb' mode .

    with open("test_file.txt",'rb') as r:
            payload = r.read()