Python requests, how to know what format data is in? - python

How do i find out what format the data i am trying to request is in?
The data can be found in the following address: https://api.coinmarketcap.com/v1/ticker/
Thank you :)

The response headers for this request include
content-type: application/json
as your browser will tell you. So it is JSON.

The Content-Type header field in the response is what you're looking for.

Related

Python requests : trying to understand form data

i am new to requests in python and i'm trying to understand what's the data I send in the request and what i'm getting back.
Firstly, to understand better, i used the network inspector on chrome and uploaded a file on the website i'm going to send requests to later (the ultimate goal is to upload my file with requests).
It starts by opening a modal window with parameters so i'm guessing in python in something as easy as this (in python):
url = 'myurl'
params = {'whatever params i need'}
export = s.get(url, params=params)
if i print the status_code of this i get 200 so i'm guessing until then it's fine.
then it sends a post to the url without any parameters but with data like this (in python):
url = 'myurl'
data= {'confused'}
export = s.get(url, data=data)
here is where i'm getting a little confused. in the network inspector the data sent looks like this :
------WebKitFormBoundaryf2WTKCh05lDGbAAG
Content-Disposition: form-data; name="form[_token]"
Kmzz8c_N9qfuo8AZ1Pd1OFgaYzE9AFtitmaLkg0-y_g
------WebKitFormBoundaryf2WTKCh05lDGbAAG
Content-Disposition: form-data; name="form[importModule]"; filename="myfile.xml"
Content-Type: text/xml
------WebKitFormBoundaryf2WTKCh05lDGbAAG--
what does all this mean ? how am i supposed to write this in python ? And im guessing this "Kmzz8c_N9qfuo8AZ1Pd1OFgaYzE9AFtitmaLkg0-y_g" is the token, but how do i get in the first place too ?
thank you for your help and time !
You seem to be confused about "parameters" (query string parameters, "GET parameters", in any case the thing you use params= for in Requests) and form data.
What you see in the network inspector in the POST request is the form data (in particular, multipart/form-data data). If you inspect the form in the modal window, you'll probably find a hidden field with name="form[_token]", and a file field with name="form[importModule]".
To emulate that POST (with a file upload) with Requests, you'd do something like
s.post(
url="...",
data={
"form[_token]": "....",
},
files={
"form[importModule]": open("some_file.xlsx", "rb"),
},
)
To actually get the value for _token, you'd probably need to parse the response from the first GET request you do.

Python POST request with XPATH

Please help me convert a working URL request in the web browser to a python request module executable request.
My working browser request URL:
https://192.168.100.25/api?type=config&action=set&xpath=/config/devices/entry[#name=%27localhost.localdomain%27]/network/interface/ethernet/entry[#name=%27ethernet1/1%27]/layer3/ip&element=%3Centry%20name=%279.6.6.6/24%27/%3E
This device basically accepts Rest API calls in XML format. Please help me, convert this to a python requests POST request.
I have found a way to do this with Python Requests:
url = '''https://192.168.100.25/api?
type=config&action=set&xpath=/config/devices&element='''
out = requests.post(url, verify=False, auth=HTTPBasicAuth(username, password))
This is working.
Please let me know if there is an easier and proper way to do this.

Parse HTTPRequest Body from multipar/form in python

I receive a Response from the server with the next body:
body='------WebKitFormBoundarylY6hpxLHtLTD33AY\r\nContent-Disposition: form-data; name="file"; filename="language.py"\r\nContent-Type: text/x-python-script\r\n\r\n#!/usr/bin/env python\n
.....
.....
\r\n------WebKitFormBoundarylY6hpxLHtLTD33AY--\r\n'
And I want to parse this body and extract, name, filename, content-type and the full content of the file for storing.
May Be possible?
Thanks in advance.
Tornado should parse this for you; the contents will be available in self.request.files.
http://www.tornadoweb.org/en/stable/httpserver.html#tornado.httpserver.HTTPRequest.files

Django is_ajax history back

I wrote a Django view that responses ether a text/html or a application/json depending on request.is_ajax().
So far so good, but when I use my browsers history buttons, I end up getting a JSON response rather than the HTML.
I can't figure out the problem. It's true an jQuery ajax request is getting the same url after the page was loaded, but that shouldn't end up in the history, or should it?
Thanks, Joe
If you send different content depending on request.is_ajax(), you need to send Vary: X-Requested-With to the browser. That way, the browser will be able to distinguish the two kinds of response based on the value of the X-Requested-With header on the request. You can do that via:
from django.views.decorators.vary import vary_on_headers
#vary_on_headers('X-Requested-With')
def yourview(request, ...):
pass

How to send GET request including headers using python

I'm trying to build a website using web.py, which is able to search the mobile.de database (mobile.de is a German car sales website). For this I need to use the mobile.de API and make a GET request to it doing the following (this is an example from the API docs):
GET /1.0.0/ad/search?exteriorColor=BLACK&modificationTime.min=2012-05-04T18:13:51.0Z HTTP/1.0
Host: services.mobile.de
Authorization: QWxhZGluOnNlc2FtIG9wZW4=
Accept: application/xml
(The authorization needs to be my username and password joined together using a colon and then being encoded using Base64.)
So I use urllib2 to do the request as follows:
>>> import base64
>>> import urllib2
>>> headers = {'Authorization': base64.b64encode('myusername:mypassw'), 'Accept': 'application/xml'}
>>> req = urllib2.Request('http://services.mobile.de/1.0.0/ad/search?exteriorColor=BLACK', headers=headers)
And from here I am unsure how to proceed. req appears to be an instance with some methods to get the information in it. But did it actually send the request? And if so, where can I get the response?
All tips are welcome!
You need to call req.read() to call the URL and get the response.
But you'd be better off using the requests library, which is much easier to use.

Categories

Resources