Skip to content

Structured parameter marshaling

Petri Savolainen edited this page Mar 1, 2017 · 1 revision

In addition to values of simple types such as strings and numbers, also structured request and body parameters can be provided using regular YAML syntax. The syntax resembles Python, and provides automatic conversion to Python data structures out of the box. However those almost always need to be converted into JSON or XML, which in turn is then URL- or Form-encoded when doing the HTTP request.

To indicate the desired output (JSON/XML), type and value are specified for a structured parameter or body:

operations:
  test:
    request:
      params:
        address:
          value: {"street": "Yellow brick road", "city": "Oz"} 
          type: json

Alternatively, a default can be given, in which case the type and value can be omitted. Such an alternative version of the above would be:

defaults:
  structured_param_type: json
  structured_body_type: json

operations:
  test:
    request:
      params:
        address: {"street": "Yellow brick road", "city": "Oz"} 

As can be seen, the default can be specified separately for request parameters and body parameters.

Assuming the API spec has been loaded into api, parameters could then be marshaled thus:

>>> marshal_request_params(api["operations"]["test"], api["defaults"]):

Clone this wiki locally