Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make POST request using Python

Tags:

python

I am trying to make a POST request But getting this error :

Traceback (most recent call last):
  File "demo.py", line 7, in <module>
    r = requests.post(url, data=payload, headers=headers)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 87, in post
    return request('post', url, data=data, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 266, in request
    prep = req.prepare()
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 215, in prepare
    p.prepare_body(self.data, self.files)
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 338, in prepare_body
    body = self._encode_params(data)
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 74, in _encode_params
    for k, vs in to_key_val_list(data):
ValueError: too many values to unpack

This is my program :

import requests

url = 'http://www.n-gal.com/index.php?route=openstock/openstock/optionStatus'

payload = {'var:1945,product_id:1126'}
headers = {'content-type': 'application/x-www-form-urlencoded'}
r = requests.post(url, data=payload, headers=headers)

I have tried the same POST request through Advanced rest client using following data :

URL : http://www.n-gal.com/index.php?route=openstock/openstock/optionStatus payload : var=1945&product_id=1126 Content-Type: application/x-www-form-urlencoded

And it is working fine can anyone help me please...

like image 388
Vaibhav Jain Avatar asked Jan 31 '26 21:01

Vaibhav Jain


2 Answers

You have made payload a set, not a dictionary. You forgot to close the string.

Change:

payload = {'var:1945,product_id:1126'}

To:

payload = {'var':'1945','product_id':'1126'}

As it is a set, the request will thus fail.

like image 199
TerryA Avatar answered Feb 03 '26 09:02

TerryA


Try this :

import requests

url = 'http://www.n-gal.com/index.php?route=openstock/openstock/optionStatus'

payload = 'var=1945&product_id=1126'
headers = {'content-type': 'application/x-www-form-urlencoded'}
r = requests.post(url, data=payload, headers=headers)

print r.json()

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!