Take data from html form and send back with flask [duplicate] - python

This question already has answers here:
Get the data received in a Flask request
(23 answers)
Converting Flask form data to JSON only gets first value
(2 answers)
Return JSON response from Flask view
(15 answers)
Closed 4 years ago.
I want to take data from html form with the python flask and send data back at the same html file. Can you help me with an example or some tips? Thank you.

You can use request.form, like the following:
from flask import Flask, request
app = Flask(__name__)
#app.route('/path_form_submits_to', methods=['POST'])
def my_handler():
return request.form.get("field_name", "default value if field isn't there")

Related

how to pass password in flask route [duplicate]

This question already has answers here:
How can I include special characters in query strings?
(7 answers)
How to send password to REST service securely?
(2 answers)
Closed 1 year ago.
I am using flask and want to create a url which is a post request and contains "username" and "password" as a string in url
#app.route('/login/<username>/<password>')
def login(username,password):
return password
My Url should look like this
http://192.158.42.102:8080/login/iamuser/iamuser##123
But the problem is that password is truncated to "iamuser##123" to "iamuser" , How can I pass the full passowrd as string.
#app.route('/login/<username>/<string:password>')
But still it is returning "iamuser" . Can anyone resolve this issue so that I could pass any string to the url. I do not want to use the form for posting the request

Python GET Request with set-cookies [duplicate]

This question already has answers here:
How can I use cookies in Python Requests?
(4 answers)
Closed 3 years ago.
I am trying to get some data from a webpage using python requests which needs to be logged in first.The login http request "response header" contains "set-cookie" parameters which is used for the next http request of the webpage. Could any tell me how to use the set-cookie for the consecutive GET request of the webpage
Try this
session = requests.Session()
response = session.get('http://google.com')
print(session.cookies.get_dict())
Or without using sessions use
response = requests.get('http://google.com')
response.cookies

Flask request csv [duplicate]

This question already has answers here:
Get the data received in a Flask request
(23 answers)
Closed 3 years ago.
I have a csv file, which I need to post on my server, convert it to json and send it back. With JSON, you can simply do request.json(Force=True), however I am not sure how to make flask to read my csv.
So far I have this:
#application.route('/postcsv', methods=['POST'])
def csv_view():
content = request.files(force=True)
stream = io.StringIO(content.stream.read().decode("UTF-8"), newline = None)
csv_input = csv.reader(stream)
print(csv_input)
return csv_input
if __name__ == '__main__':
application.run(debug=True, host='0.0.0.0')
The error I am getting is TypeError: 'ImmutableMultiDict' object is not callable. I think my approach overall is wrong but I am not sure
You got this error because request.files is not a function and thus can't be called.
Instead, you should use request.files[<KEY>]. See: Not able to parse a .csv file uploaded using Flask

cannot retrieve post data from python flask [duplicate]

This question already has answers here:
How can I use JQuery to post JSON data?
(6 answers)
How to get POSTed JSON in Flask?
(13 answers)
Get the data received in a Flask request
(23 answers)
Closed 4 years ago.
This is jquery ajax snippet use to send requests to localhost:5000/query
so this is my jquery snippet
var saveData=$.ajax({
type:"POST",
url:"http://localhost:5000/query",
datatype:"json",
data:{"type":"Company","name":"harshit","cin":"2014UPTC20"},
success:function(resultData){
alert(resultData);
}
});
and i cannot retrieve above sent data from python flask:-
#app.route('/query',methods=['POST'])
def query_neo_dynamic():
data="currently working"
#return json.dumps(fetch_specific_data(type,name,i_num))
req_data=request.get_json()
return json.dumps(json.dumps(req_data))
if __name__=="__main__":
app.run()
I am getting a null
please help

Mailgun forwarding e-mail as POST data to Python Flask [duplicate]

This question already has answers here:
Get the data received in a Flask request
(23 answers)
Closed 5 years ago.
I can't figure this one out, it's not in the request.JSON and the request.data is coming up as empty bytes in the debug. There appears to be a post but the data is disappearing? Is this a bug with Flask?
Here's a link to their documentation page that has a Django example: https://documentation.mailgun.com/en/latest/quickstart-receiving.html#supported-actions-for-routes
Figured this out: Clue was in the header
'Content-Type': 'application/x-www-form-urlencoded'
Flask automatically strips out form data into request.form leaving request.data and request.json empty:
#app.route("/test-mail/", methods=["POST"], strict_slashes=False)
def test_mail():
print(request.form)
return 'OK'

Categories

Resources