Web API POST parameters with Python
Hi, I've been updating our Python-based system to use the POST parameters as per your recent instructions, but I can't get more than one feature to update in one request. I've included my test script below. As you can see from the commented out sections I can update either the "name" or "expires" feature but only if I don't pass the name and value fields as lists.
If I try to update both features at once using lists it doesn't work, although using the echo option shows both fields (output also below), so it does look like my request is correct. Whichever way I try I always get “stat ok” as the result. Am I missing something?
Thanks,
Robin
# TEST SCRIPT
parms = {
"method": "limelm.pkey.setDetails",
# "method": "limelm.test.echo",
"api_key": API_KEY,
"format": "json",
"nojsoncallback": "1",
"pkey_id": "12345678",
"email": "a@b.com",
# works
#"feature_name": "name",
#"feature_value": "Test"
# works
#"feature_name": "expires",
#"feature_value": "2024-10-10 16:55:00"
# doesn't work
"feature_name": ["name","expires"],
"feature_value": ["Test2","2024-09-09 17:30:00"]
}
data = urllib.parse.urlencode(parms).encode()
req = urllib.request.Request(API_URL, headers=HTTP_HEADERS, data=data)
context = ssl.create_default_context(cafile=certifi.where())
response = urllib.request.urlopen(req, context=context)
print(response.readlines())
# ECHO OUTPUT
[b'{"method":{"_content":"limelm.test.echo"},"api_key":{"_content":"xxxxxxxxxxxxxxx.xxxxxxx"},"format":{"_content":"json"},"nojsoncallback":{"_content":"1"},"pkey_id":{"_content":"12345678"},"email":{"_content":"a@b.com"},"feature_name":{"_content":"[\'name\', \'expires\']"},"feature_value":{"_content":"[\'Test2\', \'2024-09-09 17:30:00\']"},"stat":"ok"}']
Well, it depends on the “magic” whatever library you're using does to create the fields to be submitted.
Here's how we require array arguments:
feature_name[]=feature1&feature_name[]=feature2&feature_value[]=value1&feature_value[]=value2