0

I am posting a array

$name[0] = "anurag";
$name[1] = "abhishek"; 
$ch = curl_init('http://localhost:8000/v0/url-generator');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "name=$name");
curl_exec ($ch);
curl_close ($ch);

In python I got name = array .

if request.method == 'POST':
        name = request.POST["name"]

print name

output = array.

I would like to output this in python:

name = ['anurag','abhishek']
1
  • what you are trying to do REST request ? Commented Feb 11, 2013 at 8:04

2 Answers 2

1

Implode $name in your PHP so that you send a string to the curl request instead of an array

http://PHP.net/implode

And then split the value in python. Or use json arrays

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

Comments

0

First use json_encode($name) send this value to the python program

If you are sending the REST body so in python side you can try like

try:
    body = self.request.body
    body_loads = loads(body)
except Exception as e:
    LOG_WARNING('Exception: %s', str(e))
    return WebReturn(self,httplib.FORBIDDEN, {'reason':'Body is not Define '},{'data':"Msg"})
    get = None
get = dict2obj(body_loads)

Here is dict2obj

def dict2obj(d):
        if isinstance(d, list):
            d = [dict2obj(x) for x in d]
        if not isinstance(d, dict):
            return d
        class C(object):
            pass
        o = C()
        for k in d:
            o.__dict__[k] = dict2obj(d[k])
        return o

Hpe this will help you

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.