3

I need to generate same output in Python as I have in PHP, possible?

Php code:

<?php 

$items = array(
   array(
   'item_key_a' => 'item_value_a')
);

$payload = array(
   'i_key' => 'i_value',
   'items' => $items,
);

echo http_build_query($payload);
 ?>

Output urlencoded:

i_key=i_value&items%5B0%5D%5Bitem_key_a%5D=item_value_a

Output urldecoded:

i_key=i_value&items[0][item_key_a]=item_value_a

Python code:

item = {}
item['item_key_a'] = 'item_value_a'

data = {}
data['i_key'] = 'i_value'
data['items'] = [item]

import urllib
print urllib.urlencode(data)

Output urlencoded:

items=%5B%7B%27item_key_a%27%3A+%27item_value_a%27%7D%5D&i_key=i_value

Output urldecoded:

items=[{'item_key_a':+'item_value_a'}]&i_key=i_value

So in Python I don't get valid urlencoding that I need for PHP app.

0

2 Answers 2

1

Python urlencode does not handle nested dict. You need to write it yourself in a manner such as presented for this question:

Sign up to request clarification or add additional context in comments.

1 Comment

yeah, saw that answer, doesn't help me, well ok will write my own
1

There now seems to be a library for handling this in Python

https://github.com/uber/multidimensional_urlencode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.