params is for GET-style URL parameters, and data is for POST-style body information. It is perfectly legal to provide both types of information in a request, and your request does so too, but you encoded the URL parameters into the URL already.
Your raw post contains JSON data though. Requests can handle JSON encoding for you, and it'll set the correct Content-Type header too; all you need to do is pass in the Python object to be encoded as JSON into the json keyword argument.
You could split out the URL parameters as well:
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
Then post your data with:
import requests
url = 'http://192.168.3.45:8080/api/v2/event/log'
data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
requests.post(url, params=params, json=data)
The json keyword is new in Requests version 2.4.2; if you still have to use an older version, encode the JSON manually using the json module and post the encoded result as the data key; you will have to explicitly set the Content-Type header in that case:
import requests
import json
headers = {'content-type': 'application/json'}
url = 'http://192.168.3.45:8080/api/v2/event/log'
data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
requests.post(url, params=params, data=json.dumps(data), headers=headers)
Answer from Martijn Pieters on Stack Overflowparams is for GET-style URL parameters, and data is for POST-style body information. It is perfectly legal to provide both types of information in a request, and your request does so too, but you encoded the URL parameters into the URL already.
Your raw post contains JSON data though. Requests can handle JSON encoding for you, and it'll set the correct Content-Type header too; all you need to do is pass in the Python object to be encoded as JSON into the json keyword argument.
You could split out the URL parameters as well:
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
Then post your data with:
import requests
url = 'http://192.168.3.45:8080/api/v2/event/log'
data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
requests.post(url, params=params, json=data)
The json keyword is new in Requests version 2.4.2; if you still have to use an older version, encode the JSON manually using the json module and post the encoded result as the data key; you will have to explicitly set the Content-Type header in that case:
import requests
import json
headers = {'content-type': 'application/json'}
url = 'http://192.168.3.45:8080/api/v2/event/log'
data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
requests.post(url, params=params, data=json.dumps(data), headers=headers)
Assign the response to a value and test the attributes of it. These should tell you something useful.
response = requests.post(url, params=data, headers=headers)
response.status_code
response.text
status_codeshould just reconfirm the code you were given before, of course
Oddity in handling params in `requests.get`
httprequest - Python Request Post with param data - Stack Overflow
oauth - Constructing requests with URL Query String in Python - Stack Overflow
How can I filter the result of the api call "Get projects paginated" using the QUERY PARAMETERS in python?
Videos
params is for GET-style URL parameters, data is for POST-style body information. It is perfectly legal to provide both types of information in a request, and your request does so too, but you encoded the URL parameters into the URL already.
Your raw post contains JSON data though. requests can handle JSON encoding for you, and it'll set the correct Content-Type header too; all you need to do is pass in the Python object to be encoded as JSON into the json keyword argument.
You could split out the URL parameters as well:
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
then post your data with:
import requests
url = 'http://192.168.3.45:8080/api/v2/event/log'
data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
requests.post(url, params=params, json=data)
The json keyword is new in requests version 2.4.2; if you still have to use an older version, encode the JSON manually using the json module and post the encoded result as the data key; you will have to explicitly set the Content-Type header in that case:
import requests
import json
headers = {'content-type': 'application/json'}
url = 'http://192.168.3.45:8080/api/v2/event/log'
data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
requests.post(url, params=params, data=json.dumps(data), headers=headers)
Assign the response to a value and test the attributes of it. These should tell you something useful.
response = requests.post(url,params=data,headers=headers)
response.status_code
response.text
- status_code should just reconfirm the code you were given before, of course
To perform GET requests with URL query string:
import requests
params = {
'action': 'subscribe',
'callbackurl': '',
'comment': '',
'oauth_consumer_key': '',
'oauth_nonce': '',
# more key=value pairs as appeared in your query string
}
r = requests.get("http://wbsapi.withings.net/notify", params=params)
With that cleared, now you just need to follow the workflow documented on http://www.withings.com/en/api/oauthguide and implement them
Upon receiving your OAuth Key and OAuth Secret, perform a
GETrequest with the following endpoint and query string which will give you backtoken:https://oauth.withings.com/account/request_token? oauth_callback=http%3A%2F%2Fexample.com%2Fget_access_token &oauth_consumer_key=c331c571585e7c518c78656f41582e96fc1c2b926cf77648223dd76424b52b &oauth_nonce=f71972b1fa93b8935ccaf34ee02d7657 &oauth_signature=J8xzgFtHTsSRw8Ejc8UDV2jls34%3D &oauth_signature_method=HMAC-SHA1 &oauth_timestamp=1311778988 &oauth_version=1.0
Then you need to authorize the token you received with the following request which will give you the user_id:
https://oauth.withings.com/account/authorize? oauth_callback=http%3A%2F%2Fexample.com%2Fget_access_token &oauth_consumer_key=c331c571585e7c518c78656f41582e96fc1c2b926cf77648223dd76424b52b &oauth_nonce=369f9ceb2f285ac637c9a7e9e98019bd &oauth_signature=OR9J9iEl%2F2yGOXP2wk5c2%2BWtYvU%3D &oauth_signature_method=HMAC-SHA1 &oauth_timestamp=1311778988 &oauth_token=5bb105d2292ff43ec9c0f633fee9033045ed4643e9871b80ce586dc1bf945 &oauth_version=1.0
Then you need to request the
access_tokenby hitting this endpoint with some more query string:https://oauth.withings.com/account/access_token? oauth_consumer_key=c331c571585e7c518c78656f41582e96fc1c2b926cf77648223dd76424b52b &oauth_nonce=7acd22371fc56fd8a0aaf8416f79f84f &oauth_signature=jmj1g%2FB3rYR2DCpWp86jB5YVHIM%3D &oauth_signature_method=HMAC-SHA1 &oauth_timestamp=1311778988 &oauth_token=5bb105d2292ff43ec9c0f633fee9033045ed4643e9871b80ce586dc1bf945 &oauth_version=1.0 &userid=831
Now you have everything needed to perform the aforementioned request in your question, and others, example directly from the documentation:
http://wbsapi.withings.com/measure? action=getmeas &oauth_consumer_key=c331c571585e7c518c78656f41582e96fc1c2b926cf77648223dd76424b52b &oauth_nonce=accbac1b7ee2b86b828e6dc4a5a539b2 &oauth_signature=XfobZMboIg2cRyNKAvyzONHHnKM%3D &oauth_signature_method=HMAC-SHA1 &oauth_timestamp=1311842514 &oauth_token=887557411788d5120537c6550fbf2df68921f8dd6f8c7e7f9b441941eb10 &oauth_version=1.0 &userid=831
Again, everything can be done without explicit oauth library as you can finish the workflow with requests.get and query string built from a dict feed into the params argument of the method.
I truly hope this helps you achieve your goal.
Here's a working example using the rauth client library. Full disclosure, I'm the original rauth author. Hope this helps:
from rauth import OAuth1Service
withings = OAuth1Service(
name='withings',
consumer_key='fd5fe4002db502983fbd056fdf416941d83e15ecb68ee9eeb4978cb2370c',
consumer_secret='29dbc46056c530814c2debcf24c76ff42f6cc66d0e3e5cfdef1e166725c6f',
base_url='http://wbsapi.withings.net/notify',
request_token_url='https://oauth.withings.com/account/request_token',
authorize_url='http://oauth.withings.com/account/authorize',
access_token_url='https://oauth.withings.com/account/access_token')
request_token, request_token_secret = withings.get_request_token()
callback = 'https://github.com/litl/rauth'
authorize_url = withings.get_authorize_url(request_token,
oauth_callback=callback)
print('Visit this URL in your browser: {url}'.format(url=authorize_url))
userid = raw_input('Enter userid from browser URL: ')
sess = withings.get_auth_session(request_token,
request_token_secret,
params={'userid': userid})
print sess.get('measure', params={'action': 'getmeas',
'userid': userid}).json()
You cannot use requests for this; the library builds such URLs if passed a Python structure for the parameters, but does not offer any tools to parse them. That's not a goal of the project.
Stick to the urllib.parse method to parse out the parameters. Once you have a dictionary or list of key-value tuples, just pass that to requests to build the URL again:
try:
# Python 3
from urllib.parse import urlparse, parse_qs
except ImportError:
# Python 2
from urlparse import urlparse, parse_qs
o = urlparse(url)
query = parse_qs(o.query)
# extract the URL without query parameters
url = o._replace(query=None).geturl()
if 'token' in query:
query['token'] = 'NEW_TOKEN'
requests.get(url, params=query)
You can get both the urlparse and parse_qs functions in both Python 2 and 3, all you need to do is adjust the import location if you get an exception.
Demo on Python 3 (without the import exception guard) to demonstrate the URL having been built:
>>> from urllib.parse import urlparse, parse_qs
>>> url = "http://httpbin.org/get?token=TOKEN_TO_REPLACE¶m2=c"
>>> o = urlparse(url)
>>> query = parse_qs(o.query)
>>> url = o._replace(query=None).geturl()
>>> if 'token' in query:
... query['token'] = 'NEW_TOKEN'
...
>>> response = requests.get(url, params=query)
>>> print(response.text)
{
"args": {
"param2": "c",
"token": "NEW_TOKEN"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.5.1 CPython/3.4.2 Darwin/14.1.0"
},
"origin": "188.29.165.245",
"url": "http://httpbin.org/get?token=NEW_TOKEN¶m2=c"
}
Using requests only:
query = requests.utils.urlparse(url).query
params = dict(x.split('=') for x in query.split('&'))
if 'token' in params:
params['token'] = 'NEW_TOKEN'
requests.get(url, params=params)
So, if I understand correctly, you can use get to get back a response of some basic HTML? And then you can add get parameters to change what you get in the response? (Like maybe a URL parameter could be saying you want filtering videos by new which would return different html than filtering by hot)?
And them I am kind of confused about post and put, because how do you know what you want post something to a website..
payload = {'username': 'Franklin', 'password': 'testing'}r = requests.post('https://httpbin.org/post', data=payload)
In this example, if it was a real website, how would I know what the payload keys should be? Do I have to look through the HTML?
Same thing goes with the Put method.
TLDR: How do I know what paramaters/values/keys/whatevers I need to post or put to a website for a request?
Thanks for the help!
Been learning more about accessing APIs and stuff. I keep running into things about params and json to do requests. I don't exactly understand what the difference between these two is. I use the same dictionaries but the keyword differs.
I tried to read the documentation but its a bit more technical than my ability.