pass post request using url generated by flask - flask

I am using a flask tutorial that I found here...
https://andrewgriffithsonline.com/blog/180412-deploy-flask-api-any-serverless-cloud-platform/
In the "Test the App" section, there is a "http-prompt" command used. I do not want to use that. Instead I will like to use python requests module.

Related

flask url_for behind a reverse proxy

I have a flask application running on a server (192.168.1.1:8080) located behind a reverse proxy. Let's say, the url https://foo.bar.com/myapp point to my flask app, i.e. to 192.168.1.1:8080.
I am in trouble with the url_for flask function, as it returns something like http://192.168.1.1:8080/blabla (giving blabla as parameter).
How to proceed so that url_for returns https://foo.bar.com/myapp/blabla instead ?
In fact, my application uses a CAS authentication system. So that the ticket gets validate by the CAS server, I need to provide an URL of the shape https://foo.bar.com/....
Any help would be appreciate.
I found a solution using ProxyFix from werkzeug :
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app, x_host=1)

How to link Django and React URLs to perform actions?

In Django, I have my login URL set to 'api/auth/login'. When given a username and password, it will log that user in. Running 'python manage.py runserver', it will put that URL at 'http://127.0.0.1:8000/api/auth/login'
However, my React project, when running 'yarn start' is at 'http://localhost:3000/' and giving it the extension 'api/auth/login', the url it attempts is 'http://localhost:3000/api/auth/login'.
This does not work, but if I replace the URL extension with 'http://127.0.0.1:8000/api/auth/login', the login works as expected.
How can I have the URLs work with my React app? Or is this not something that necessarily needs to be fixed? I do plan to host this project somewhere and am not yet sure how the URLs will work out..
One option is to set proxy in the package.json.
Second option is to set axois baseURL:
// add in your code before main component definition
// for example in App.js
import axios from "axios";
axios.defaults.baseURL = "http://127.0.0.1:8000";
I'm preferring the second approach. For doing production build it can be easily overwritten with an environment variable.
import axios from "axios";
axios.defaults.baseURL = REACT_APP_SERVER_URL
Remember to set the CORS headers in the Django server (I'm working on tutorial with an example how to do this).
You are hosting react application on another port that's why when you put a request without mentioning host, it gets appended to your current host i.e. 127.0.0.1:3000. I suggest you write "proxy": '127. 0.0.1:8000' in your package.json (refer https://create-react-app.dev/docs/proxying-api-requests-in-development/) and restart your react server or use full url of django server i.e. 127.0.0.1:8000/

HighCharts doesnt get data from Flask endpoint

I use the code of "Live data from dynamic CSV" from Highcharts Demos as a basis for my need. As it gets data lively from a web service url, I just change it to get data from my Flask endpoint. That is to say, I use the same code to be served with Flask.
Even if the flask endpoint serves the same content, I don't get it to work. I just changed the url of "https://demo-live-data.highcharts.com/time-data.csv" with /data. And here /data serves the totally same kind of csv output.
So far I could not obtain the result.
Demo code located in "https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/live-data/" is used.
var defaultData = '/data';
I found out that I have to use "http://10.0.2.2:5000/data" instead of "0.0.0.0:5000/data". Because 10.0.2.2 is the special alias to host loopback interface (aka 127.0.0.1).

How to add apispec for post method in flask MethodView?

I have the following packages in my project:
1. flask (webframework)
2. webargs and marshmallow for request and response definition
3. apispec and apispec-webframework.flask for generation of openapi 3.0 doc.
Our project has chosen to use MethodView from flask to define APIs. One of our API looks like the following.
POST /resource/<resource-id>
{
"attribute-1": <attribute-value1>,
"attribute-2": <attribute-value1>,
}
The apispec documentation lists how to specify responses but there is no clear way of defining the input from path (resource-id) and request body (attributes).
https://apispec.readthedocs.io/en/stable/using_plugins.html?highlight=MethodView
I would appreciate if someone could clarify how to use webargs and marshmallow to define the input for a MethodView:post() method for the above API.

given a list of urls how can i return the content of that urls from a generator asyncronously using twisted python

I want to know how to return the content of a list of urls asynchronously using twisted in python. I know i can use getPage() to get the url content asynchronously but how can i use the result with yield of the generator to return the result from the generator function.
synchronous code looks like this
import requests
def gen(urls):
for url in urls:
yield requests.get(url)
Edit 1:
my specific requirement is to provide a service through flask python. which is, given a keyword my flask application should return the content of all urls related to that keyword. i can get list of urls from a keyword using a search engine api, all i have to do is return the content using server sent events (event source) as streaming service.
def handle_request():
urls=search_engine.search(requests.args.get('query'))
def content_gen():
for url in urls:
yield requests.get(url)
return Response(content_gen(), mimetype="text/event-stream")
the requests.get call is synchronous, all i want is to make the code asynchronous by using getPage() of twisted for my flask application
EDIT 2:
by using Twisted's getPage on all urls i am going to get a list of deferreds. Flask is a synchronous framework, so i cant directly use deferreds to return data through Flask. By using crochet library i can wait on deferreds synchronously since waiting on deferred is blocking using #wait.for decorator in crochet, the results are returned sequentially. But i want the generator function to yield the data of url whichever is available rather than following the url sequence.
Honestly I don't know much about crochet or twisted, so if i am asking a trivial question please excuse me.