0

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

  • @SiHa In case the attached image is not clear, the error is : HTTPError: Unsupported Media Type – Neha Bhandari Nov 30 '20 at 21:00
  • @NehaBhandari The image doesn't actually mention `Unsupported Media Type` and it seems as if not the whole error has been posted. – Mitchell van Zuylen Nov 30 '20 at 21:05
  • @MitchellvanZuylen : sorry i missed the last line while taking the snip. – Neha Bhandari Nov 30 '20 at 21:09
  • Here is the error - Traceback (most recent call last): File "", line 1, in 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) – Neha Bhandari Nov 30 '20 at 21:11
  • 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 – Neha Bhandari Nov 30 '20 at 21:11
  • You are URL-encoding the data of the request; this is only necessary if you are going to pass this data in the URL itself (e.g. as a query argument). Are you sure the API in question requires that, or does it just accept JSON? In that case you should be able to call `urllib.request.urlopen(url, data=json.dumps(your_dict))` – Iguananaut Nov 30 '20 at 21:12
  • @NehaBhandari You can edit your question after posting. ~Please edit your question and paste the error traceback there, not in comments.~ No worries, I went ahead and did it. – Iguananaut Nov 30 '20 at 21:13
  • @Iguananaut Thanks for your response. I need to pass the data in url itself. i tried this way urllib.request.urlopen(url, data=json.dumps(your_dict)) , but getting error: TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str. – Neha Bhandari Nov 30 '20 at 21:23
  • Add `.encode('utf-8')` after `json.dumps()`. It would be unusual for a POST method to take data in the URL itself. – Iguananaut Nov 30 '20 at 21:39
  • @Iguananaut i tried this way urllib.request.urlopen(GENERIC_PREDICTIVE_URL, data=json.dumps(your_dict).encode('utf-8')) , again getting the error : HTTPError: Unsupported Media Type – Neha Bhandari Nov 30 '20 at 21:47
  • Without knowing more about the API you're posting to and what it expects it's impossible to say. You might also have to pass a `Content-Type: application/json` header, for example. If indeed it even accepts JSON. – Iguananaut Nov 30 '20 at 21:51
  • Also, when i try another api which has payload in the format {"a":"some text" ,"b:"some text"} the code i shared (in the issue description) works fine but if the payload is nested it giving the error - HTTPError: Unsupported Media Type – Neha Bhandari Nov 30 '20 at 21:54
  • @Iguananaut let me know what details of the api you might require i will try to share it with you if possible – Neha Bhandari Nov 30 '20 at 22:09
  • Well do you know for sure if it accepts JSON in the POST body? – Iguananaut Nov 30 '20 at 23:27
  • Yes it it accepts JSON in POST body – Neha Bhandari Dec 01 '20 at 03:32

1 Answers1

0

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)
  • Maalik Watto : this also gives error {"detail":"Unsupported media type \"application/x-www-form-urlencoded\" in request."} . It works fine for another api which accepts data in format {"a":"some text","b":"some text"} – Neha Bhandari Dec 01 '20 at 03:57