2

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...

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried your solution but getting the same error ...what i can see from the traceback is request is failing my side actually no request is made ....i am getting error in my code there is something wrong with my code....
5

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

Comments

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

Comments

0

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.

Comments

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.