简体   繁体   中英

Python requests Module - Passing Nested XML as Query Parameters

I'm trying to structure an HTTP PUT request using the Python requests module. For all of my purposes thus far, I've been able to construct a dictionary with the XML element data that I'd like to send, which I then pass as query parameters. My code looks like the following:

import requests

scan_params = {
    'type': 'T2 FSE 3D MICE'
    'quality': 'usable'
}
resp = requests.put(url, params=scan_params)

This works fine. However, when I want to pass nested XML elements, or XML attributes, I'm not sure how to include these as parameters in scan_params. What I'd like to do is something like:

import requests

scan_params = {
    'type': 'T2 FSE 3D MICE'
    'quality': 'usable',
    'parameters/te': 3.1
}
resp = requests.put(url, params=scan_params)

Where te is a child of parameters . Likewise, I'm not sure how to specify attributes in this dictionary.

I want to avoid using some XML library to construct well-formed XML, since that will be an additional development step.

Any ideas for what I should be doing? Thanks!

To code nested XML elements OR attributes as parameters in a query string, you can use a forward slash.

Example:

scan_params = {
    'type': 'T2 FSE 3D MICE'
    'quality': 'usable',
    'parameters/te': 3.1,
    'parameters/voxelRes/x': 1.0
}

where te is an element of parameters , and x is an attribute of voxelRes , which is itself an element of parameters .

I'm not sure what would happen if an element had an attribute AND an element with the same name, or whether that would even be considered well-formed XML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM