It worked by setting the Content-Type
header to Content-Type: application/x-www-form-urlencoded
instead of Content-Type: application/xml
.
cURL XML POST works but not Python Requests - 3rd party Expert Rating API (w/ examples)
-
04-07-2023 - |
Pergunta
I'm trying to make a POST request that sends XML data to a 3rd party server, it works with cmd line curl but not the python requests library (which I need to work). I've omitted out the username, password, and domain urls. I'm using requests==2.2.1
CURL EXAMPLE (WORKS):
Request:
$ curl --data '<?xml version="1.0" encoding="UTF-8"?><request><authentication partnerid="12345" password="mypass1234" /><method name="GetTestList" /></request>' 'http://dev.expertrating.com/mydomain/webservices/'
Response:
<response><result name="GetTestList"><records><record test_id="6683" test_name="Demo Adobe Photoshop CS3 Test" coverage="Layers ;Type ;Automating Tasks and Keyboard Shortcuts ;Workspace ;Working with Images, Retouching and Transforming ;Color Management ;Color and Tonal Adjustments ;Using Selection and Drawing ;Filters and Saving Images ;Working With Web, Video and Animation" total_questions="10" duration="10" passing_marks="60" category="Demo Tests" /><record test_id="6684" test_name="Demo Programming with C++ Test" coverage="Classes ;Constructors ;Variables and Datatypes" total_questions="10" duration="10" passing_marks="60" category="Demo Tests" /><record test_id="6685" test_name="Demo HTML 4.01 Test" coverage="Advanced Tags ;Fundamentals ;Tags ;Tables ;Links and Images ;Forms and Frames" total_questions="10" duration="10" passing_marks="60" category="Demo Tests" /><record test_id="6682" test_name="Demo Editing Skills Certification" coverage="Prepositions ;Parallel Construction ;Comma Usage ;Punctuation ;Misplaced Modifiers ;Apostrophe Usage ;Sentence Structure ;Pronoun Usage ;Tenses ;Article Usage" total_questions="10" duration="10" passing_marks="60" category="Demo Tests" /></records></result></response>
REQUESTS EXAMPLE: (FAILS) (via ipython/django)
IPython:
In [1]: import requests
In [2]: payload = '<?xml version="1.0" encoding="UTF-8"?><request><authentication partnerid="12345" password="mypass1234" /><method name="GetTestList" /></request>'
In [3]: r = requests.post('http://dev.expertrating.com/mydomain/webservices/', data=payload, headers={'Content-Type': 'application/xml'})
In [4]: r.text
Out[4]: u"<?xml version='1.0' encoding='UTF-8'?><response><error id='101' message='Invalid Request'><info>You have not posted a valid request.</info></error></response>"
Am I encoding the requests string wrong? Including the wrong headers? I literally copy/pasted the urls and payload xml. I also tried POSTing with urllib2 with similar failing results.
Any help will be greatly appreciated, thanks in advance to anyone who reads this.
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow