I see you are using requests library. From version 2.4.2 onwards, you can use json as parameter. It should have better json encoding than data.
requests.post('https://getpocket.com/v3/send', json={
'consumer_key': consumer_key,
'access_token': access_token,
'actions': [{
'item_id':'1000000000',
'action':'archive'
}]
})
See here for more information: http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests
EDIT: If only action key should be a json string, you can try following:
import simplejson as json
requests.post('https://getpocket.com/v3/send', data={
'consumer_key': consumer_key,
'access_token': access_token,
'actions': json.dumps([{
'item_id':'1000000000',
'action':'archive'
}])
})
I would recommend simplejson over json package as it is updated more frequently.
I see you are using requests library. From version 2.4.2 onwards, you can use json as parameter. It should have better json encoding than data.
requests.post('https://getpocket.com/v3/send', json={
'consumer_key': consumer_key,
'access_token': access_token,
'actions': [{
'item_id':'1000000000',
'action':'archive'
}]
})
See here for more information: http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests
EDIT: If only action key should be a json string, you can try following:
import simplejson as json
requests.post('https://getpocket.com/v3/send', data={
'consumer_key': consumer_key,
'access_token': access_token,
'actions': json.dumps([{
'item_id':'1000000000',
'action':'archive'
}])
})
I would recommend simplejson over json package as it is updated more frequently.
From your example:
https://getpocket.com/v3/send?\
actions=[{"action":"archive","time":1348853312,"item_id":229279689}]&\
access_token=ACCESS_TOKEN&\
consumer_key=CONSUMER_KEY
we see there are three URL-encoded parameters, only one of which (actions) is a JSON value. You need to use something like
actions = {
"action": "archive",
"time": "1348853312",
"item_id": "229279689"
}
requests.post("https://getpocket.com/v3/send",
data={
"actions": json.dumps(actions),
"access_token": access_token,
"consumer_key": consumer_key
})
How to parse JSON Array of objects in python - Stack Overflow
How to send an array using requests.post (Python)? "Value Error: Too many values to unpack" - Stack Overflow
arrays - Python 3 requests library and json within a list - Stack Overflow
How do I store JSON response to Array - Python request - Stack Overflow
It is because you pass an request object in the json.loads
In the error it says that it needs a string or buffer instead. You can pass in the string from the request by doing:
json.loads(platformReq.text)
But platformReq.json() works the same way!
Hope that helps.
EDIT
To respond to your status code question (sorry didn't see it before). You can check the response status code like this:
if platformReq.status_code == requests.codes.ok:
# Print the response
print platformReq.json()
else:
print "Something went wrong";
If you are expecting a JSON response, the code that you have commented out should work:
json.loads(platformReq.text)
The error you are getting is because json.loads() needs a string, not a response object.
As for the error handling I would recommend adding a check at the top of this for platformReg.status_code to make sure that it is equal to 200 before you proceed
Take a look at the json module. More specifically the 'Decoding JSON:' section.
import json
import requests
response = requests.get() # api call
users = json.loads(response.text)
for user in users:
print(user['id'])
You can try like below to get the values from json response:
import json
content=[{
"username": "admin",
"first_name": "",
"last_name": "",
"roles": "system_admin system_user",
"locale": "en",
"delete_at": 0,
"update_at": 1511335509393,
"create_at": 1511335500662,
"auth_service": "",
"email": "[email protected]",
"auth_data": "",
"position": "",
"nickname": "",
"id": "pbjds5wmsp8cxr993nmc6ozodh"
}, {
"username": "chatops",
"first_name": "",
"last_name": "",
"roles": "system_user",
"locale": "en",
"delete_at": 0,
"update_at": 1511335743479,
"create_at": 1511335743393,
"auth_service": "",
"email": "[email protected]",
"auth_data": "",
"position": "",
"nickname": "",
"id": "akxdddp5p7fjirxq7whhntq1nr"
}]
for item in content:
print("Name: {}\nEmail: {}\nID: {}\n".format(item['username'],item['email'],item['id']))
Output:
Name: admin
Email: [email protected]
ID: pbjds5wmsp8cxr993nmc6ozodh
Name: chatops
Email: [email protected]
ID: akxdddp5p7fjirxq7whhntq1nr
You want to pass in JSON encoded data. See the API documentation:
Remember — All post bodies must be JSON encoded data (no form data).
The requests library makes this trivially easy:
headers = {"W-Token": "Ilovemyboss"}
data = [
{
'url': '/rest/shifts',
'params': {'user_id': 0, 'other_stuff': 'value'},
'method': 'post',
},
{
'url': '/rest/shifts',
'params': {'user_id': 1,'other_stuff': 'value'},
'method':'post',
},
]
requests.post(url, json=data, headers=headers)
By using the json keyword argument the data is encoded to JSON for you, and the Content-Type header is set to application/json.
Well, It turns out that all I needed to do was add these headers:
headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
and then call requests
requests.post(url,data=json.dumps(payload), headers=headers)
and now i'm good!