user5594493
user5594493

Reputation: 1130

Python Requests POST not working

I am using python requests module for calling API's. Everything was working fine until I pushed my code to AWS. Even on AWS it is working if I am working on 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 not allowed GET method so in response I am getting error that GET method not allowed which means requests.post is reads as get

Any idea what’s wrong here.

Upvotes: 6

Views: 17063

Answers (1)

user5594493
user5594493

Reputation: 1130

Actually the issue was due to SSL , if your server is using https method then you need to add following line in 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 have written a small function for that

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

Upvotes: 6

Related Questions