Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending post request with both data and JSON parameters

I am trying to send a post request with both data and JSON fields, such as:

my_df = pd.read_csv('test.csv')
pickled = pickle.dumps(my_df)
pickled_b64 = base64.b64encode(pickled)

my_name = "my_csv_name.csv"

r = requests.post('http://localhost:5003/rcvdf', data = pickled_b64, json= {"name": my_name})

Notice that pickled_b64 is of type 'bytes'.

But on the other end, I get only the data and no JSON at all:

@api.route('/rcvdf', methods=['POST'])
def test_1():
    data_pickeled_b64_string = request.data
    json_data = request.json #This is None

Is it even possible to pass two variables in such a way?

--edit

Solution

thanks to the comments I tried different approach - and passed the variable by headers:

Sending Side:

my_name = "my_csv.csv"

r = requests.post('http://localhost:5003/rcvdf', data=pickled_b64, headers={"name": my_name})

Receiving Side:

@app.route('/rcvdf', methods=['POST'])
def rcvdf():
    data_pickeled_b64_string = request.data
    name = request.headers['name']
like image 733
Daniel Fridman Avatar asked Jun 11 '26 15:06

Daniel Fridman


1 Answers

requests.post(url, json=payload) is just a shortcut for requests.post(url, data=json.dumps(payload))

Note, the json parameter is ignored if either data or files is passed.

docs

So, no, it is not possible to pass both data and json parameters.

like image 174
SergiyKolesnikov Avatar answered Jun 13 '26 18:06

SergiyKolesnikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!