Python requests, how to calculate Content-Length and add host to convert request from Postman

680 Views Asked by At

Using the request liberally I would like convert my POST request from Postman to the Python function. My currently code returns "('Invoice must be in xml format', 400)" . I'm not shore if I should add the following keys: Content-Length and Host. If yes, how to calculate these values in the header?
Postman requests Headers

Body

My Python code

import requests

url3 = 'http://xxx.azure.com/xxx/api/file'
file = 'ok/test_file.xml'

headers = {'Token': 'xxx',
            'Content-Type': 'application/xml'}

body = {'Tags': 'python',
        'ExternalTrackingId': '1-python',
        'FastTrack': False, 
        'RequiresBatchSend': True,
        'file.xml': (open(file, 'rb'), 'text/xml')}

def send_invoice():
    send_file = requests.post(url3, headers=headers, data=body)
    return send_file.text, send_file.status_code

print(send_invoice())

Response

('File must be in xml format', 400)


How to improve my code to correctly send the XML file to the API?

1

There are 1 best solutions below

3
Muhammad Sholeh On

In postman you can inspect your request and read the code request with almost all languages including python requests.