1

I am trying to pass a complicated dict into a Python requests.

dict={
 'form':'parse_category',
 'category':{
  'category':1,
  'text':'cat',
  'plural_text':'cats'
 },
 'sources':[1,2,3,4,5,6]
}

category=requests.post('http://localhost/trigger.php',json=dict)
print category.text

I would like to get a nice multidimensional $_POST variable on trigger.php

Desired result:

$_POST=[
 'form'=>'parse_category',
 'category'=>[
  'category'=>1,
  'text'=>'cat',
  'plural_text'=>'cats'
 ],
 'sources'=>[1,2,3,4,5,6]
];

I learned that I need to use json=dict or some type of json encoded variable. What method do I use to get the JSON data on the PHP side? $_POST and $_GET return empty.

2
  • 1
    Did you ever discover how to do this? Commented Aug 29, 2021 at 18:02
  • @MawgsaysreinstateMonica I answered my question below. Commented Oct 2, 2021 at 16:51

1 Answer 1

0

You can create a JSON parameter:

import json
category=requests.post('http://localhost/trigger.php',params={'json':json.dumps(array)})

Then decode the json on the php side:

if(isset($_GET['json'])){$_POST=json_decode($_GET['json']);}
Sign up to request clarification or add additional context in comments.

1 Comment

this is incorrect. You sent a POST request, but received a GET.

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.