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.
To create a POST request in Python, use the requests. post() method. The requests post() method accepts URL. data, json, and args as arguments and sends a POST request to a specified URL.
You'll want to adapt the data you send in the body of your request to the specified URL. Syntax: requests. post(url, data={key: value}, json={key: value}, headers={key:value}, args) *(data, json, headers parameters are optional.)
JSON Payload Example [Python Code] A request payload is data that clients send to the server in the body of an HTTP POST, PUT, or PATCH message that contains important information about the request.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With