Search code examples
pythondictionarypyramidurl-parameters

URL query parameter as a dictionary in Pyramid


The JSON API specification uses a dictionary like syntax for some query parameters:

GET /articles?include=author&fields[articles]=title,body,author&fields[people]=name HTTP/1.1

Is there any easy way to retrieve the above fields parameter as a dictionary using Pyramid?

To provide further details, the following fields parameters are given:

  • fields[articles]=title,body,author
  • fields[people]=name

I'd like to get them all in a Python dictionary:

fields = {
    'articles': 'title,body,author',
    'people': 'name',
}

Solution

  • If I understand what you are asking, an example is in the Pyramid Quick Tour with links to further examples and the complete Pyramid request API.

    Essentially:

    fields_articles = request.params.get('fields[articles]', 'No articles provided')
    fields_people = request.params.get('fields[people]', 'No people provided')
    

    Then put them into a dictionary.

    EDIT

    Would this suffice?

    fields = {
        'articles': request.params.get('fields[articles]', ''),
        'people': request.params.get('fields[people]', ''),
    }