Added http_request method

This commit is contained in:
Felipe Martin 2020-03-13 10:44:17 +01:00
parent 23f895c5a5
commit e06eb57f7f
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
2 changed files with 26 additions and 1 deletions

24
app.py
View File

@ -1,5 +1,6 @@
import os
import requests
from flask import Flask, request, Response, render_template, jsonify
app = Flask(__name__)
@ -82,5 +83,28 @@ def log_view():
return Response(data)
@app.route("/http_request", methods=["POST"])
def http_request_view():
"""
Performs an http request.
The view is just a passthrough to the requests library:
- `method` parameter to set the request method
- Every other parameter will be sent directly to the requests request method
"""
params = request.json
method = params.pop("method", "GET")
response = requests.request(method, **params)
return jsonify({
"method": method,
"params": params,
"response": {
"content": response.text,
"json": response.json(),
"headers": {key: value for key, value in response.headers.items()}
}
})
if __name__ == "__main__":
app.run(debug=True, port=8080, host="0.0.0.0")

View File

@ -1 +1,2 @@
flask==1.1.1
flask
requests