Vaibhav Jain
Vaibhav Jain

Reputation: 5507

Make POST request using 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...

Upvotes: 2

Views: 1907

Answers (4)

Guilherme Coppini
Guilherme Coppini

Reputation: 122

I know this is a really old question, but I had the same problem when using Docker recently, and managed to solve it by including the requests library in the requirements (or just update the library with pip install requests --upgrade).

When using the original version of the requests library, it raised that very same error on Python3.8, which only stopped after upgrading it.

Upvotes: 0

user2458922
user2458922

Reputation: 1721

import requests
import json

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=json.dumps(payload), headers=headers)

Upvotes: 0

user2173955
user2173955

Reputation:

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()

Upvotes: 5

TerryA
TerryA

Reputation: 59974

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.

Upvotes: 6

Related Questions