Why does this Python POST Request returns 400 Bad Request Code?

Viewed 255

I have a Python script that does a POST Request sending an xml to a link and it returns 400 Bad Request. Where do you think is the problem ? In the xml ?

import base64
import requests
import os

proxy = 'http://26:Do7@proxy.f.c:8080'

os.environ['http_proxy'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTP_PROXY'] = proxy
os.environ['HTTPS_PROXY'] = proxy

with open("exemplu.pdf","rb") as pdf_file:
encoded_string = base64.b64encode(pdf_file.read())
b64 = encoded_string
#print(b64)

xml = """"<header xmlns="mfp:anaf:dgti:spv:reqStareMesaj:v1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<listaMesaje index_incarcare="390"></listaMesaje>
</header>"""


headers = {'Content-Type':'application/xml','Authorization':'Bearer     f73e'}
x = requests.post('https://api.anaf.ro/SPVWS2/rest/stareMesaj',data=xml,headers=headers)
print(x.status_code)
print(x.content)
#print(xml) 

I would like to obtain a 200 Code in return

Thanks in advance

2 Answers

Chnage:

xml = """"

To:

xml = """ 

You have four " instead of three.

Printing

xml = """"<header xmlns="mfp:anaf:dgti:spv:reqStareMesaj:v1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<listaMesaje index_incarcare="390"></listaMesaje>
</header>"""

print(xml)

(ignoring the erroneous syntax coloring here) results in

"<header xmlns="mfp:anaf:dgti:spv:reqStareMesaj:v1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<listaMesaje index_incarcare="390"></listaMesaje>
</header>

wich is invalid xml.

Related