В поисках решения вот этой проблемы выяснил, что проблема в API, оно принимает не JSON, а в отправляемых данных должен быть URL text. Вопрос такой, можно ли преобразовать такой JSON:
JSONFORMDATA = {
"add": [
{
"source_name": "WEB сайт",
"source_uid": "a1fee7c0fc436088e64ba2e8822ba2b3",
"created_at": "1529007000",
"incoming_entities": {
"leads": [
{
"name": "Покупка"
}
],
"contacts": [
{
"name": "Федя",
"responsible_user_id": "1903006",
"custom_fields": [
{
"id": "382707",
"values": [
{
"value": "+77777777777",
"enum": "WORK"
}
]
},
{
"id": "389993",
"values": [
{
"value": "sfgh3gh233h3h3h3"
}
]
},
{
"id": "389995",
"values": [
{
"value": "Обратный звонок"
}
]
}
]
}
]
},
"incoming_lead_info": {
"form_id": "329248",
"form_page": "vdtest.ru",
"ip": "127.0.0.1",
"service_code": "QkKwSam8"
}
}
]
}
в такой формат:
add[0][source_name]=WEB FORM
&add[0][source_uid]=wb0123456789
&add[0][created_at]=1529105760
&add[0][incoming_entities][leads][0][name]=WEB Bay
&add[0][incoming_entities][contacts][0][name]=John
&add[0][incoming_entities][contacts][0][responsible_user_id]=2473552
&add[0][incoming_entities][contacts][0][custom_fields][0][id]=382707
&add[0][incoming_entities][contacts][0][custom_fields][0][values][0][value]=+792887776655
&add[0][incoming_entities][contacts][0][custom_fields][0][values][0][enum]=MOB
&add[0][incoming_entities][contacts][0][custom_fields][1][id]=389993
&add[0][incoming_entities][contacts][0][custom_fields][1][values][0][value]=cid123456789
&add[0][incoming_entities][contacts][0][custom_fields][2][id]=389995
&add[0][incoming_entities][contacts][0][custom_fields][2][values][0][value]=Product
&add[0][incoming_lead_info][form_id]=329248
&add[0][incoming_lead_info][form_page]=localhost
&add[0][incoming_lead_info][ip]=127.0.0.1
&add[0][incoming_lead_info][service_code]=myformmw
Это декодированные данные из URL text, которые принимает API:
add%5B0%5D%5Bsource_name%5D=WEB+FORM&add%5B0%5D%5Bsource_uid%5D=wb0123456789&add%5B0%5D%5Bcreated_at%5D=1529105760&add%5B0%5D%5Bincoming_entities%5D%5Bleads%5D%5B0%5D%5Bname%5D=WEB+Bay&add%5B0%5D%5Bincoming_entities%5D%5Bcontacts%5D%5B0%5D%5Bname%5D=John&add%5B0%5D%5Bincoming_entities%5D%5Bcontacts%5D%5B0%5D%5Bresponsible_user_id%5D=2473552&add%5B0%5D%5Bincoming_entities%5D%5Bcontacts%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bid%5D=382707&add%5B0%5D%5Bincoming_entities%5D%5Bcontacts%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bvalues%5D%5B0%5D%5Bvalue%5D=%2B792887776655&add%5B0%5D%5Bincoming_entities%5D%5Bcontacts%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bvalues%5D%5B0%5D%5Benum%5D=MOB&add%5B0%5D%5Bincoming_entities%5D%5Bcontacts%5D%5B0%5D%5Bcustom_fields%5D%5B1%5D%5Bid%5D=389993&add%5B0%5D%5Bincoming_entities%5D%5Bcontacts%5D%5B0%5D%5Bcustom_fields%5D%5B1%5D%5Bvalues%5D%5B0%5D%5Bvalue%5D=cid123456789&add%5B0%5D%5Bincoming_entities%5D%5Bcontacts%5D%5B0%5D%5Bcustom_fields%5D%5B2%5D%5Bid%5D=389995&add%5B0%5D%5Bincoming_entities%5D%5Bcontacts%5D%5B0%5D%5Bcustom_fields%5D%5B2%5D%5Bvalues%5D%5B0%5D%5Bvalue%5D=Product&add%5B0%5D%5Bincoming_lead_info%5D%5Bform_id%5D=329248&add%5B0%5D%5Bincoming_lead_info%5D%5Bform_page%5D=localhost&add%5B0%5D%5Bincoming_lead_info%5D%5Bip%5D=127.0.0.1&add%5B0%5D%5Bincoming_lead_info%5D%5Bservice_code%5D=myformmw
Как выяснилось, в примере PHP не используют чистый JSON, а строят массив. Может в Python тоже необходимо построить подобный массив или есть способ проще, ведь на сколько я понимаю, как таковых массивов в Python нет.
Ответ
Честно, рекурсию не очень люблю, но в таких задачах она себя хорошо показывает:
Данные (из вопроса):
JSON_FORM_DATA = {
"add": [
...
}
Код:
def dict_to_url_params(json_data, root):
def deep(node, root, items):
if isinstance(node, list):
for i, value in enumerate(node):
node_root = root + '[{}]'.format(i)
deep(value, node_root, items)
elif isinstance(node, dict):
for key, value in node.items():
node_root = root + '[{}]'.format(key)
deep(value, node_root, items)
else:
root += '=' + node
items.append(root)
items = []
deep(json_data, root, items)
return items
if __name__ == '__main__':
items = dict_to_url_params(JSON_FORM_DATA['add'], root='add')
params = '&'.join(items)
print('Params: ' + params)
print()
print('Params:')
for x in items:
print(x)
Результат:
Params: add[0][source_name]=WEB сайт&add[0][source_uid]=a1fee7c0fc436088e64ba2e8822ba2b3&add[0][created_at]=1529007000&add[0][incoming_entities][leads][0][name]=Покупка&add[0][incoming_entities][contacts][0][name]=Федя&add[0][incoming_entities][contacts][0][responsible_user_id]=1903006&add[0][incoming_entities][contacts][0][custom_fields][0][id]=382707&add[0][incoming_entities][contacts][0][custom_fields][0][values][0][value]=+77777777777&add[0][incoming_entities][contacts][0][custom_fields][0][values][0][enum]=WORK&add[0][incoming_entities][contacts][0][custom_fields][1][id]=389993&add[0][incoming_entities][contacts][0][custom_fields][1][values][0][value]=sfgh3gh233h3h3h3&add[0][incoming_entities][contacts][0][custom_fields][2][id]=389995&add[0][incoming_entities][contacts][0][custom_fields][2][values][0][value]=Обратный звонок&add[0][incoming_lead_info][form_id]=329248&add[0][incoming_lead_info][form_page]=vdtest.ru&add[0][incoming_lead_info][ip]=127.0.0.1&add[0][incoming_lead_info][service_code]=QkKwSam8
Params:
add[0][source_name]=WEB сайт
add[0][source_uid]=a1fee7c0fc436088e64ba2e8822ba2b3
add[0][created_at]=1529007000
add[0][incoming_entities][leads][0][name]=Покупка
add[0][incoming_entities][contacts][0][name]=Федя
add[0][incoming_entities][contacts][0][responsible_user_id]=1903006
add[0][incoming_entities][contacts][0][custom_fields][0][id]=382707
add[0][incoming_entities][contacts][0][custom_fields][0][values][0][value]=+77777777777
add[0][incoming_entities][contacts][0][custom_fields][0][values][0][enum]=WORK
add[0][incoming_entities][contacts][0][custom_fields][1][id]=389993
add[0][incoming_entities][contacts][0][custom_fields][1][values][0][value]=sfgh3gh233h3h3h3
add[0][incoming_entities][contacts][0][custom_fields][2][id]=389995
add[0][incoming_entities][contacts][0][custom_fields][2][values][0][value]=Обратный звонок
add[0][incoming_lead_info][form_id]=329248
add[0][incoming_lead_info][form_page]=vdtest.ru
add[0][incoming_lead_info][ip]=127.0.0.1
add[0][incoming_lead_info][service_code]=QkKwSam8
Комментариев нет:
Отправить комментарий