2

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',
}
Spack
  • 464
  • 4
  • 22
  • You're gonna need to parse this format yourself. From a quick review of available JSON-API libraries for Python there doesn't seem to be a widely available parser for this format. As @stevepiercy's answer below indicates, there is a semi-hacky way to do it directly from the parsed x-www-urlencoded format that webob understands. – Michael Merickel Jul 18 '17 at 18:45

1 Answers1

3

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]', ''),
}
Steve Piercy
  • 13,693
  • 1
  • 44
  • 57