Python Flask, connect another url - python

I simplify my requirement to this simple code
from flask import Flask, request
import requests
app = Flask(__name__)
#app.route('/', methods=['POST'])
def root():
payload = {'Message': 'yo','Port':'123'}
r = requests.post("http://127.0.0.1:5000/test", data=payload)
return r.text
#app.route('/test', methods=['POST'])
def test():
return request.form['Message']+','+request.form['Port']
if __name__ == '__main__':
app.run(debug=True)
I've tested the url '127.0.0.1:5000/test' by google's Postman.
I send the data to the url, and It worked, can return the result I wanted.
And I created another .py to test the url, too. It also can show the result I wanted.
import requests
payload = {'Message':'yo','Port':'123'}
r = requests.post("http://127.0.0.1:5000/test", data=payload)
print r.text
Then I put the same code below
#app.route('/', methods=['POST'])
def root():
I want to use the url '127.0.0.1:5000/' to send data to the '127.0.0.1:5000/test' (I also Use google's Postman)
And it can't worked... it always show 'loading...'
Is there a better way to implement my requirement ?
I will thank you so so so much ~!!

Instead of sending a local POST request, which would take a lot longer time, just have them both route to the same function, and set defaults.
#app.route('/', methods=['GET'])
#app.route('/test', methods=['POST'])
def test():
return request.form.get('Message', 'yo') + ',' + request.form.get('Port', '123')
Which would respond normally to all POST requests on /test, and with a GET to /, it works the same as POSTing {'Message': 'yo', 'Port': '123'} to /test

Related

Can't change the HEAD on a repo via Gerrit API

I'm using python requests in order to make an API call and change the HEAD of the repo on Gerrit but for some reason it doesn't work. I've tried to call it with Postman and works fine but from my code i can't.
from flask import Flask
import requests
app = Flask(__name__)
#app.route("/test", methods=["PUT"])
def hello_world():
headers = {"Content-Type":"application/json"}
password = "khOGam97z5hKR+zmONVOx23bvlFROANtzc5hTaCNQQ"
response = requests.put("http://localhost:8080/a/projects/myproject/HEAD", data={ "ref": "refs/heads/test"}, auth=("admin", password), headers=headers)
return response.reason
if __name__=="__main__":
app.run()

My #app.route decorator is not triggering its function

I am new to python and flask and I am encountering a problem with Flask. I am trying to use a local HTTP POST (webhook) to call a function from another file, but when I do nothing happens.
from flask import Flask
from BotSpeak import main
app = Flask(__name__)
#app.route('/', methods=['POST'])
def respond():
main('hello')
if __name__ == '__main__':
app.run()
This is my very basic Flask app. As you can see it is trying to call the main function from this file (BotSpeak):
from json import dumps
from httplib2 import Http
def main(botmsg):
url = 'PLACEHOLDER FOR GOOGLE CHAT WEBHOOK URL'
bot_message = {
'text' : botmsg}
message_headers = {'Content-Type': 'application/json; charset=UTF-8'}
http_obj = Http()
response = http_obj.request(
uri=url,
method='post',
headers=message_headers,
body=dumps(bot_message),
)
print(response)
if __name__ == '__main__':
main("TEST MESSAGE")
This is the code that shoots local HTTP POSTs to my flask app:
import json
import requests
webhook_url ='http://127.0.0.1:5000/'
data = {PLACE HOLDER FOR JSON DATA}
r = requests.post(webhook_url, data=json.dumps(data), headers={'Content-Type': 'application/json'})
I can call the main function from other files outside the flask app and it'll work but it just wont trigger in the app.route decorator like it should. I really appreciate anyone's help and I encourage you to throw these into VS Code and try it for yourself.
If you're using flask you don't have to manually construct an HTTP response using httplib2 unless you have some very specific use case.
In your case you can just return bot_message since if the return value of a Flask handler is a dict it will convert it to a JSON response (including the correct headers). See
https://flask.palletsprojects.com/en/2.0.x/quickstart/#about-responses
In other words, your entire route handler could be rewritten (based off your example at least):
#app.route('/', methods=['POST'])
def respond():
return {'text': 'hello'}
If you want to pass the handling off to some utility function you can do that too, just make sure it returns a value, and that you in turn return an appropriate value from your respond() function.

Flask server can't read cyrillic requests

Here is my simple endpoint:
#app.route('/test', methods=['POST'])
def test():
print(request.data)
return Response("", mimetype='application/json')
The request body is:
"ั‚ะตัั‚"
And the server prints b'"\xd1\x82\xd0\xb5\xd1\x81\xd1\x82"' for request data.
How to set appropriate charset, providing content type header did nothing?
Also is there a way to get rid of that annoying b at the start of every request body?
For anyone having similar problem:
from flask import Flask
from flask import Response
from flask import reques
import chardet
#app.route('/test', methods=['POST'])
def test():
charset = chardet.detect(request.data)['encoding']
print(request.data.decode(charset))
return Response("", mimetype='application/json')
I was unable to make flask to this for me...
so you have to manually decode the bytes.

How to get json data from another website in Flask?

I want to make a website with Flask to show some data posted from another website such as this one.In short,I want to list the data from that website.
Should I use this method: flask.Request.get_json()?
I do not know how to get a Request object. Could you show me some demos about that?
I am using Python 3 and Flask.
import requests
def get(url):
try:
res = requests.get(url)
return res.json()
except:
return False
data = get('http://example.com')
print(data)
In short,I want to list the data from that website.
The accepted answer seem not use flask, so I'll add some:
from flask import jsonify, Flask
import requests
app = Flask(__name__)
#app.route('/')
def index():
r = requests.get('https://api.github.com/users/runnable')
return jsonify(r.json())
Should I use this method: flask.Request.get_json()
No, it is for parsing the incoming json request data. No one is sending requests to you.

Reading POST body with bottle.py

I am having trouble reading a POST request with bottle.py.
The request sent has some text in its body. You can see how it's made here on line 29: https://github.com/kinetica/tries-on.js/blob/master/lib/game.js.
You can also see how it's read on a node-based client here on line 4: https://github.com/kinetica/tries-on.js/blob/master/masterClient.js.
However, I haven't been able to mimic this behavior on my bottle.py-based client. The docs say that I can read the raw body with a file-like object, but I can't get the data neither using a for loop on request.body, nor using request.body's readlines method.
I'm handling the request in a function decorated with #route('/', method='POST'), and requests arrive correctly.
Thanks in advance.
EDIT:
The complete script is:
from bottle import route, run, request
#route('/', method='POST')
def index():
for l in request.body:
print l
print request.body.readlines()
run(host='localhost', port=8080, debug=True)
Did you try simple postdata = request.body.read() ?
Following example shows reading posted data in raw format using request.body.read()
It also prints to the log file (not to the client) raw content of body.
To show accessing of form properties, I added returning "name" and "surname" to the client.
For testing, I used curl client from command line:
$ curl -X POST -F name=jan -F surname=vlcinsky http://localhost:8080
The code which works for me:
from bottle import run, request, post
#post('/')
def index():
postdata = request.body.read()
print postdata #this goes to log file only, not to client
name = request.forms.get("name")
surname = request.forms.get("surname")
return "Hi {name} {surname}".format(name=name, surname=surname)
run(host='localhost', port=8080, debug=True)
Simple script for processing POSTed data. POSTed data are written in a terminal and returned to the client:
from bottle import get, post, run, request
import sys
#get('/api')
def hello():
return "This is api page for processing POSTed messages"
#post('/api')
def api():
print(request.body.getvalue().decode('utf-8'), file=sys.stdout)
return request.body
run(host='localhost', port=8080, debug=True)
Script for POSTing json data to the script above:
import requests
payload = "{\"name\":\"John\",\"age\":30,\"cars\":[ \"Ford\", \"BMW\",\"Fiat\"]}"
url = "localhost:8080/api"
headers = {
'content-type': "application/json",
'cache-control': "no-cache"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)

Categories

Resources