0

I have a simple rest api which I want to send a post request to using requests. And I am trying to send a post request using:

req = urllib2.Request('http://ip-adress:8990/DATA')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json.dumps(final_out))

However this returns a 405

when i run this i got this error?

urllib2.HTTPError HTTPError: HTTP Error 405: METHOD NOT ALLOWED

1 Answers1

1

Use POST method of requests library and you will achieve exactly what you need.

>>> import requests
>>> r = requests.post("http://bugs.python.org", data={'number': 12524, 'type': 'issue', 'action': 'show'})
>>> print(r.status_code, r.reason)
200 OK
>>> print(r.text[:300] + '...')

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
Issue 12524: change httplib docs POST example - Python tracker

</title>
<link rel="shortcut i...
>>> 

Thanks to @user816328 for a great answer. Link to answer

Federico Rubbi
  • 714
  • 3
  • 16
Hassan Raza
  • 45
  • 10