Saturday 21 November 2020

How to pass a nested params object for post method of requests library in python3?

I want to create a nested object to pass to requests.post method as a param. This is my code -

import requests

baseURL = "http://ec2-3-6-40-236.ap-south-1.compute.amazonaws.com/civicrm/sites/all/modules/civicrm/extern/rest.php?"
userkey = "secret"
sitekey = "secret"


def create(payload):
    r = requests.post(baseURL, params=payload)
    print(r.url)


def createCustomPayload(customPayload):
    payloadCommon = {'action': 'create', 'api_key': userkey, 'key': sitekey}
    payloadCommon.update(customPayload)
    return payloadCommon


create(createCustomPayload({'entity': 'CustomField', 'json': {'custom_group_id': 'test_16Nov_Voter', 'label': 'Relationship With Guardian',
                                                              'data_type': 'String', 'html_type': 'Radio', 'weight': 7, 'options_per_line': 2, 'option_values': ["Father", "Mother", "Husband", "Other"]}}))

It prints this URL-

http://ec2-3-6-40-236.ap-south-1.compute.amazonaws.com/civicrm/sites/all/modules/civicrm/extern/rest.php?action=create&api_key=secret&key=secret&entity=CustomField&json=custom_group_id&json=label&json=data_type&json=html_type&json=weight&json=options_per_line&json=option_values

It should ideally print this URL-

http://ec2-3-6-40-236.ap-south-1.compute.amazonaws.com/civicrm/sites/all/modules/civicrm/extern/rest.php?entity=CustomField&action=create&api_key=secret&key=secret&json={"custom_group_id":"test_16Nov_Voter","label":"Relationship With Guardian","options_per_line":2,"data_type":"String","html_type":"Radio","weight":7,"option_values":["Father","Mother","Husband","Other"]}

Please help.



from How to pass a nested params object for post method of requests library in python3?

No comments:

Post a Comment