# requires pip install requests
import requests
# webpage url to sent request to
url = 'https://www.google.com'
# send get requests
x = requests.get(url=url)
# print status code of request
print(x.status_code)
# print webpage source code text
print(x.text)
# print webpage as json
print(x.json)
# print webpage headers
print(x.headers)
# send a get request and timeout if it takes longer than 2.5 seconds
x = requests.get(url=url, timeout=2.5)
# demonstrate how to use the 'params' parameter: query string
x = requests.get(url=url, params={"model": "Mustang"})
####################
# require pip install requests
import requests
url = 'https://www.google.com'
form_data = {'username': 'john', 'password': "pwd"}
# send post request
x = requests.post(url=url, json=form_data)
# send post request and timeout if it takes longer than 3 seconds
x = requests.post(url=url, json=form_data, timeout=3)
# use the 'auth' parameter to send requests with HTTP Basic Auth:
x = requests.post(url, data=from_data, auth=('user', 'pass'))
print(x.text)
print(x.headers)
print(x.status_code)
# first do: pip install flask
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['POST'])
def result():
print(request.data) # raw data
print(request.json) # json (if content-type of application/json is sent with the request)
print(request.get_json(force=True)) # json (if content-type of application/json is not sent)
import requests
import logging
API_KEY = "xxxxxxxxxxxxxxxxxxxxxxx"
API_ENDPOINT = "https://sample.endpoint.php"
try:
# your source data eg format
source_data = {"key":list(values)}
# header to be sent to api
headers = {"api-key" : API_KEY}
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, json=source_data, headers=headers)
logging.info("Data push was completed with status code :"+str(r.status_code))
except requests.exceptions.RequestException as e:
logging.info("error occured : "+ str(e) +"
status code:"+str(r.status_code))