I'm new to Python and in need of some help. My aim is to send some XML with a post request to a URL, which is going to trigger a SMS being sent.
I have a small XML document that I want to post to the URL. Can I reference the XML document on my server in the python code that needs posting, or do I include the XML data to be sent in the actual python code. Can any help me out with an example?
If you need to send XML I would recommend that you take a look at requests. It allows you to easily send data using POST requests.
You should be able to transmit the XML data directly from your Python code using requests.
xml = """my xml"""
headers = {'Content-Type': 'application/xml'}
requests.post('http://www.my-website.net/xml', data=xml, headers=headers)
You could also load the xml from a text-file and send that, if you don't want to have the xml document hard-coded.
If you don't want use an outside library, you can just urllib2. See this answer for an example of how to do so.
To extract the XML from the file you just have to do
XML_STRING = open('path/to/xml_file').read()
Related
I am using Python request method for sending a Post API request.
The content of the response obtained is in the following format:
<RESPONSE>
<DATE></DATE>
<CODE></CODE>
</RESPONSE>
How to proceed in parsing the response if I want to get the code from the response?
This looks like XML; parse it using an XML library?
Depending on the situation, you could also ignore that it's XML and just pluck out the pieces that you need; this will be particularly useful if the response is not in fact valid XML, but risks summoning eldritch horrors.
I am trying to generate a request and response template from a WSDL.
I know SOAP UI can do this, but I need to use Python in my case.
I have seen that there is a python lib called zeep, but it seems to be targeted more towards setting up a soap client and performing calls.
How can I get the request from the WSDL?
Here is an example I have been using:
https://www.tutorialspoint.com/wsdl/wsdl_example.htm
You can create the request XML documents with zeep, see http://docs.python-zeep.org/en/master/client.html#creating-the-raw-xml-documents
I am writing a web service using Django that will be consumed from a MS SharePoint workflow. In the SP workflow, I created a dictionary with 2 items (id:1, text:'foo'), and used this dictionary as the request content. However, instead of using the dictionary to format a traditional POST parameter list, it sends it as a JSON object in the body of the POST request, so instead of the expected:
id=1&text=foo
in the body of the request, there is this:
{"id":1,"text":"foo"}
which of course, in turn, does not get parsed correctly by Python/Django (I am not sure who exactly does the parsing). How can I either get it to parse JSON, or get SharePoint to send traditionally encoded POST parameters?
EDIT
I saw other posts that explain how to get the raw body and parse the JSON. I was looking for a solution that would either:
Make SharePoint send normal data, or
Get Django to respect the Content-type header that states the data is JSON
There is no need for any parsing at the framework level. The body of the post request is always available in request.body, so you can access it directly:
result = json.loads(request.body)
May be it will help you bit more to handle.
import json
import urlparse
json.dumps(urlparse.parse_qs("id=1&text=foo"))
I am having a bit of trouble understanding API calls and the URLs I'm supposed to use for grabbing data from Imgur. I'm using the following URL to grab JSON data, but I'm receiving old data: http://imgur.com/r/wallpapers/top/day.json
But if I strip the .json from the end of the URL, I see the top pictures from today.
All I want is the JSON data from the top posts of today from Imgur, but keep getting data the refers to Dec 18th, 2014.
I'm using the call in a Python script. I have a token from Imgur to do the stuff, and reading the API documentation, I see a lot of the examples start with https://api. instead of http://imgur.
Which one should I use?
It's probably due to cache control, you can set it to no-cache with your headers and send along with your requests.
Sample (I'm using requests):
import requests
r = requests.get('http://imgur.com/r/wallpapers/top/day.json',
headers={'Cache-Control': 'no-cache'})
# ... your stuff here ...
Imgur updated their docs, so the new and correct form of the URL I used was:
r = requests.get("https://api.imgur.com/3/gallery/r/earthporn/top/")
I already have binary data read from a file. Most of the examples I see online link directly to the file, and upload the whole file. I am looking how to upload the binary data that I already have from another source via HTTP POST in python.
Alternatively:
req = urllib2.Request("http://example.com", data, {'Content-Type': 'application/octet-stream'})
urllib2.urlopen(req)
That also shows how you can specify the Content-Type of the data.
I'm not sure what online examples you're looking at, but urllib2.urlopen takes the data to post as a chunk of data and not a file at all.