作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个嵌套对象作为参数传递给 requests.post 方法。
这是我的代码 -
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"]}}))
它打印这个 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
理想情况下,它应该打印此 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"]}
请帮忙。
最佳答案
在我看来,好像你想通过 'json'
下的对象键作为json字符串:
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
关于python - 如何在python3中为请求库的post方法传递嵌套的params对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64857388/
我是一名优秀的程序员,十分优秀!