sandy
sandy

Reputation: 529

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.

Upvotes: 3

Views: 1202

Answers (3)

iampritamraj
iampritamraj

Reputation: 316

Its seems u r trying to send data from unauthorised sources and its also possible to throw error if website is cloudflare protect..as i see u neither use session id nor cookies... that's why u sending proper data but it's flagged by server as unauthorised request.. please share website and payload if u want proper help...u can also use request-html instead of using request library... Here

Upvotes: -1

Yorgos Katsaros
Yorgos Katsaros

Reputation: 161

In order to send a JSON object as a query parameter through an HTTP request, you need to serialize it first.

You won't get the URL you're expecting to.
Instead, you'll get an encoded version of the JSON object, passed as a query parameter.

See here for more on json serialization.

Upvotes: 0

Patrick Artner
Patrick Artner

Reputation: 51623

It seems to me as if you want to pass the object under the 'json' key as json string:

def createCustomPayload(customPayload):
    import json
    if "json" in customPayload:
        # convert the object into a json string and attach under the json key
        customPayload["json"] = json.dumps(customPayload["json"])
    
    payloadCommon = {'action': 'create', 'api_key': userkey, 'key': sitekey}
    payloadCommon.update(customPayload)

    return payloadCommon

Upvotes: 3

Related Questions