0

I have string like:

{
    'singleQuesResponses': {
        '28': '',
        '14': '',
        '27': '',
        '26': ''
    },
    'org_apache_struts_taglib_html_TOKEN': 'a1553f25303435076412b5ca7299b936',
    'quesResponses': {
        'some': 'data'
    }
}

when i post it in python requests like:

data = json.loads(data)
page = requests.post('http://localhost/chilivery/index.php', data=data, cookies=bcookies)

then value that post is something like this:

array(3) { ["quesResponses"]=> string(4) "some" ["singleQuesResponses"]=> string(2) "14" ["org_apache_struts_taglib_html_TOKEN"]=> string(32) "a1553f25303435076412b5ca7299b936" }

but i expect:

array(3) { ["quesResponses"]=> array["some"=>'data'] ["singleQuesResponses"]=> string(2) "14" ["org_apache_struts_taglib_html_TOKEN"]=> string(32) "a1553f25303435076412b5ca7299b936" }

I mean why 'some' is not sent by value as array and the only first key of it send as a string ?

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
  • This doesn't look like django... are you sure its not php?.. – Sayse Sep 16 '16 at 07:59
  • i send data with requests.post django to php remote –  Sep 16 '16 at 08:08
  • Looks like a bug somewhere with inner dicts in your data. Maybe have a network trace to see what is actually sent over the network, that will identity which side has the bug (your code or PHP on the other side. – Guillaume Sep 16 '16 at 08:22
  • @Guillaume the php in other side is just a dd($_POST) –  Sep 16 '16 at 08:24
  • Sayse seems right, this question does not concern the Django framework (even though you may be working with it). It is rather about the `requests` module. – MarAja Sep 16 '16 at 08:25
  • @MarAja django tag is removed –  Sep 16 '16 at 08:27
  • 1
    @ user272426 you should probably try to set the header of your HTTP POST request to `{'Content-Type': 'application/json'}` and use the json module to dump your data before posting. In practice use the following line to post your data: `requests.post(url, data=json.dumps(data), headers={'Content-Type': 'application/json'}` – MarAja Sep 16 '16 at 08:34
  • @MarAja it return array(0) { } –  Sep 16 '16 at 08:37
  • @user272426 Damn, how are you reading your data on the PHP side? – MarAja Sep 16 '16 at 08:43
  • @MarAja dd($_POST) –  Sep 16 '16 at 08:49

2 Answers2

0

That's how it's supposed to work. The setting the data params as a dict will make the requests module attempt to send it with:

Content-Type: application/x-www-form-urlencoded

It will only take the first level of the dictionary and encode it in the body as x=data&y=other_data.

You should either encode your object with json.dumps(data) or assign it to the json argument directly.

requests.post(url, json=data)

This sets the Content-Type header to application/json

OR

requests.post(url, data=json.dumps(data))

This won't set the Content-Type header.

SeeknInspYre
  • 360
  • 1
  • 3
  • 16
  • requests.post(url, json=data) will not send anything –  Sep 16 '16 at 09:42
  • requests.post(url, data=json.dumps(data)) this will not send with value only the key will be send as I said in question –  Sep 16 '16 at 09:43
  • What's your data object? Is it a python dictionary, or a json string? If its already a json string, you are not doing anything wrong on the python side. What you can do to debug your request is to look [here](http://stackoverflow.com/questions/20658572/python-requests-print-entire-http-request-raw) – SeeknInspYre Sep 16 '16 at 09:57
  • it is string json.loads(data) to make it valid json –  Sep 16 '16 at 10:01
  • json.dumps(data) converts a python dict to json string, json.loads converts json string back to python object. If you can update your question with the exact code you are using to prepare your data, maybe I can point your mistake. – SeeknInspYre Sep 16 '16 at 10:06
  • my code only works when the header is set to Content-Type: application/x-www-form-urlencoded , is this problem ? –  Sep 16 '16 at 10:38
0

In this case, only way is sending your data with Get instead of posting them with data; you can use params in requests module.

Alireza Rahmani Khalili
  • 2,727
  • 2
  • 32
  • 33