2

Im trying to write a python script that basically interacts with a webservice that uses an xml api. The request method is POST.

Usually I would write a request of the form request(url, data, headers) - however, in the case of an xml api it would not work. Also something like data.encode('utf-8') or urllib.urlencode(data) would not work as the data is not a dict.

In this case, data is xml so how am i supposed to sent it over?

[EDIT] When I send a string of XML I get a urllib2.HTTPError: HTTP Error 415: Unsupported Media Type Exception. Is there any other way I'm supposed to send the data?

Also, the API I am using the Google Contacts API. I'm trying to write a script that adds a contact to my gmail account.

Ikke
  • 99,403
  • 23
  • 97
  • 120
  • 1
    How is sending a string of XML different from sending an urlencoded `dict`? – patrys Sep 27 '11 at 12:34
  • This may possibly be related - http://stackoverflow.com/questions/206154/whats-the-best-soap-client-library-for-python-and-where-is-the-documentation-fo – Ofir Sep 27 '11 at 12:41
  • When I send a string of XML I get a "urllib2.HTTPError: HTTP Error 415: Unsupported Media Type" Exception. Is there any other way I'm supposed to send the data? –  Sep 27 '11 at 13:06
  • What is the Content-type header you're using when submitting XML data? – larsks Sep 27 '11 at 15:39

1 Answers1

1

You probably need to set proper Content-Type header, for XML it would probably be:

application/xml

So something like this should get you going:

request = urllib2.Request( 'xml_api.example.com' )
request.add_header('Content-Type', 'application/xml')
response = urllib2.urlopen(request, xml_data_string)

Hope that helps :)

kgr
  • 9,750
  • 2
  • 38
  • 43