开发者

sending data to xml api of webservice

开发者 https://www.devze.com 2023-04-08 12:47 出处:网络
Im trying to write a python script that basically interacts with a webservice that uses an xml api. The request method is POST.

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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消