Neha Bhandari
Neha Bhandari

Reputation: 1

Nested Dictionaries to payload for the POST request Python

I am trying to make a Post request to an api using the below code:

import urllib.request
import urllib.parse

data = urllib.parse.urlencode({"value": [{"Start": {"Client_id": 111,"pipeline_name": "ABC","input_data": {"From": "", "To": "", "Date": "","CC": "", "Subject": "some text", "Body": ""}, "eml_file": ""}}]})
data = data.encode('utf-8', errors='ignore')
                   
with urllib.request.urlopen(GENERIC_PREDICTIVE_URL, data) as f:
    response = f.read().decode('utf-8', errors='ignore')

But I'm getting this error:

Traceback (most recent call last):
 File "<ipython-input-262-98ccec4e20b4>", line 1, in <module>
  with urllib.request.urlopen(GENERIC_PREDICTIVE_URL, data) as f:
 File "D:\ProgramFiles\lib\urllib\request.py", line 163, in urlopen
  return opener.open(url, data, timeout)
 File "D:\ProgramFiles\lib\urllib\request.py", line 472, in open
  response = meth(req, response)
 File "D:\ProgramFiles\lib\urllib\request.py", line 582, in http_response 
  'http', request, response, code, msg, hdrs)
 File "D:\ProgramFiles\lib\urllib\request.py", line 510, in error
  return self._call_chain(*args)
 File "D:\ProgramFiles\lib\urllib\request.py", line 444, in _call_chain
  result = func(*args)
 File "D:\ProgramFiles\lib\urllib\request.py", line 590, in http_error_default
  raise HTTPError(req.full_url, code, msg, hdrs, fp)
HTTPError: Unsupported Media Type

I believe this is because of the format of data but I don't understand how to encode it properly.

I tried the solution provided here, but couldn't resolve the issue.

Also, the api is giving response using postman. Adding postman screenshots also here Postman screenshot

Upvotes: 0

Views: 434

Answers (1)

Abbas Maalik Wattoo
Abbas Maalik Wattoo

Reputation: 129

You can simply try this using python requests library:

data = {"value": [{"Start": {"Client_id": 111,"pipeline_name": "ABC","input_data": {"From": "", "To": "", "Date": "","CC": "", "Subject": "some text", "Body": ""}, "eml_file": ""}}]}
response = requests.post(url, data=data)

Upvotes: 0

Related Questions