Python request does not work POST

I am using the python request module to call the API. Everything worked fine until I pushed my code to AWS. Even in AWS, it works if I work on a dev server, i.e. ec2.####.amazon.com:8000 .

Here is my code:

 r = requests.post(api_url, data = {"var 1":"value", "var 2":"value"}) 

My API url does not allow the GET method, so in response I get an error that the GET method is not allowed, which means that requests.post reads as GET

Any idea what is wrong here.

+5
source share
1 answer

In fact, the problem is with SSL, if your server uses the https method, then you need to add the following line to requests.post

 r = requests.post(api_url, data = {"var 1":"value", "var 2":"value"}, verify=True) 

Also make sure your api_url includes https not http

I wrote a small function for this

 def get_base_url(request): host = get_host(request) if request.is_secure(): return '{0}{1}/{2}'.format('https://', host, 'url') else: return '{0}{1}/{2}'.format('http://', host, 'url') 
+3
source

Source: https://habr.com/ru/post/1238839/


All Articles