0

I'm currently working on a automated way to interface with a database website that has RESTful webservices installed. I am having issues with figure out the proper formatting of how to properly send the requests listed in the following site using python. https://neesws.neeshub.org:9443/nees.html

Particular example is this:

POST https://neesws.neeshub.org:9443/REST/Project/731/Experiment/1706/Organization

<Organization id="167"/>

The biggest problem is that I do not know where to put the XML formatted part of the above. I want to send the above as a python HTTPS request and so far I've been trying something of the following structure.

>>>import httplib
>>>conn = httplib.HTTPSConnection("neesws.neeshub.org:9443")
>>>conn.request("POST", "/REST/Project/731/Experiment/1706/Organization")
>>>conn.send('<Organization id="167"/>')

But this appears to be completely wrong. I've never actually done python when it comes to webservices interfaces so my primary question is how exactly am I supposed to use httplib to send the POST Request, particularly the XML formatted part of it? Any help is appreciated.

Val Gorbunov
  • 1
  • 1
  • 1

1 Answers1

0

You need to set some request headers before sending data. For example, content-type to 'text/xml'. Checkout the few examples,

Post-XML-Python-1

Which has this code as example:

import sys, httplib

HOST = www.example.com
API_URL = /your/api/url

def do_request(xml_location):
"""HTTP XML Post requeste"""
request = open(xml_location,"r").read()
webservice = httplib.HTTP(HOST)
webservice.putrequest("POST", API_URL)
webservice.putheader("Host", HOST)
webservice.putheader("User-Agent","Python post")
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(request))
webservice.endheaders()
webservice.send(request)
statuscode, statusmessage, header = webservice.getreply()
result = webservice.getfile().read()
    print statuscode, statusmessage, header
    print result

do_request("myfile.xml")

Post-XML-Python-2

You may get some idea.

Jeroen Baert
  • 1,273
  • 2
  • 12
  • 28
Babu
  • 2,548
  • 3
  • 30
  • 47
  • and if your endpoint uses SSL make sure to change `webservice = httplib.HTTP(HOST)` to `webservice = httplib.HTTPS(HOST)` or you'll get timeouts – jeffci Jun 13 '18 at 14:20