user8589075
user8589075

Reputation:

JSON array as a single parameter value in Python's requests

I'm learning to work with requests and JSON, so I decided to use API of the app I heavily use. I've successfully made requests to receive an auth code and access token, so I can get the content with the request like this:

requests.post('https://getpocket.com/v3/get',
               data = {'consumer_key' : consumer_key,
                       'access_token' : access_token,
                         'detailType' : 'simple',
                              'count' : '50'})

But when I want to modify entries, I build the similar request, which doesn't work for some reason:

requests.post('https://getpocket.com/v3/send',
               data = {'consumer_key' : consumer_key,
                       'access_token' : access_token,
                            'actions' : [{'item_id':'1000000000','action':'archive'}]})

The API guide suggests the following valid example (I decoded it for visibility):

https://getpocket.com/v3/send?actions=[{"action":"archive","time":1348853312,"item_id":229279689}]&access_token=ACCESS_TOKEN&consumer_key=CONSUMER_KEY

I get the error "400 Bad Request" and one of the headers says: "Invalid request, please refer to API documentation".

Is there anything I'm doing wrong with the syntax?

Upvotes: 1

Views: 9826

Answers (3)

chepner
chepner

Reputation: 530773

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

Upvotes: 1

mdpt
mdpt

Reputation: 143

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.

Upvotes: 2

It seems that the API requires the parameters to be on the URL, but you're sending on the post body.

You should encode you dictionary into the URL:

from urllib.parse import urlencode

data = {
        'consumer_key' : consumer_key,
        'access_token' : access_token,
        'actions' : [{'item_id':'1000000000','action':'archive'
       }
requests.post('https://getpocket.com/v3/send/?' + urlencode(data))

Upvotes: 0

Related Questions