431 (Request Header Fields Too Large) (ReactJS/ Flask) - javascript

I'm getting a 431 error on a simple fetch post, specifically:
net::ERR_ABORTED 431 (Request Header Fields Too Large)
Proxy set to: "proxy": "http://0.0.0.0:3000" (on reactjs)
fetch("/process_image", {
method:"POST",
cache: "no-store",
headers:{
"content_type":"application/json",
},
body:JSON.stringify("testing")
}
).then(response => {
//return response.json()
})
.then(json => {
//this.setState({playerName: json[0]})
});
I have tried to clear the cache and cookies on my browser based on the solutions similar to my problem but nothing is helping, and I wouldn't think this would cause an issue since all I'm passing is a small text.
I have also tried to replicate my solution using .net core, and I am able to post data with no issues, but using .net core does not fit in my scope. (I'm learning how to use flask)
I'm passing the data to a .py file:
from flask import Flask, request, jsonify
from flask_cors import CORS
import Image, io, base64
import numpy as np
from keras import models
app = Flask(__name__)
CORS(app)
#app.route("/")
def home():
return "Hello, Flask!"
#app.route("/process_image", methods=['GET', 'POST'])
def process_image():
data_uri = request.json()
print("hello: ", data_uri)
Any ideas what could be causing the problem?

Related

Sending a POST request from Vue.js to flask

I want to send a POST request from Vue component to Flask backend.
I tried the following code:
in script.js:
finishGame: function () {
this.gameStarted = false;
this.$http.post("http://127.0.0.1:5000/", "hello")
.then(function () {
alert("POST")
});
},
in app.py:
from flask import Flask, render_template
from flask_cors import CORS
from model.words import Words
# Set up custom delimiters to avoid Vue.js conflict.
class CustomFlask(Flask):
jinja_options = Flask.jinja_options.copy()
jinja_options.update(dict(
block_start_string='(%',
block_end_string='%)',
variable_start_string='((',
variable_end_string='))',
comment_start_string='(#',
comment_end_string='#)',
))
app = CustomFlask(__name__)
CORS(app)
word_gen = Words("model/word_lists/onegin.csv")
#app.route('/', methods=['POST'])
def save_game_results():
print("Game res!")
return render_template('index.html', words=word_gen.get_random(500), words_num=word_gen.size())
#app.route('/')
def play_hat():
return render_template('index.html', words=word_gen.get_random(500), words_num=word_gen.size())
The script contains more code, but the finishGame function runs for sure and only the piece staring with this.$http is ignored. The code doesn't generate any errors, but save_game_results() is never called when finishGame() is called.
I already tried:
Replacing "http://127.0.0.1:5000/" with "/".
Replacing "http://127.0.0.1:5000/" with "http://127.0.0.1:5000/go" and #app.route('/', methods=['POST'])
with #app.route('/go', methods=['POST'])
It hasn't worked.

adding http response header

I have written a web app in Heroku in python and would like to grant javascript clients basic access to my resources. I have been reading this article on how to do that: https://www.w3.org/wiki/CORS_Enabled
and from the article I have found that I should do the following:
print("Content-Type: text/turtle")
print("Content-Location: mydata.ttl")
print("Access-Control-Allow-Origin: *")
procfile is as follows: web: python app.py
and app.py is as follows:
#!/usr/bin/env python
import gevent.monkey
gevent.monkey.patch_all()
import bottle
import os
#bottle.route('/')
def index():
print("Content-Type: text/turtle")
print("Content-Location: mydata.ttl")
print("Access-Control-Allow-Origin: *")
return("testing")
bottle.run(server='gevent', host='0.0.0.0', port=os.environ.get('PORT', 5000))
however I still can't access the resources, I am getting this error:
Failed to load https://ksmlgames.herokuapp.com/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
thanks for the help
#!/usr/bin/env python
import gevent.monkey
gevent.monkey.patch_all()
import bottle
import os
#bottle.route('/')
def index():
response.set_header("Content-Type", "text/turtle")
response.set_header("Content-Location", "mydata.ttl")
response.set_header("Access-Control-Allow-Origin", "*")
return("testing")
bottle.run(server='gevent', host='0.0.0.0', port=os.environ.get('PORT', 5000))
#thmsdnnr, This seems to be working, thanks
You'll want to use response.set_header.
def index():
response.set_header("Content-Type", "text/turtle")
response.set_header("Content-Location", "mydata.ttl")
response.set_header("Access-Control-Allow-Origin", "*")
return("testing")
If you find yourself doing this for many routes, you can set up an 'after_request' hook like this.

Scraping javascript page with PyQt5 and QWebEngineView

I'm trying to render a javascripted webpage into populated HTML for scraping. Researching different solutions (selenium, reverse-engineering the page etc.) led me to this technique but I can't get it working. BTW I am new to python, basically at the cut/paste/experiment stage. Got past installation and indentation issues but I'm stuck now.
In the test code below, print(sample_html) works and returns the original html of the target page but print(render(sample_html)) always returns the word 'None'.
Interestingly, if you run this on amazon.com they detect it is not a real browser and return html with a warning about automated access. However the other test pages provide true html that should render, except it doesn't.
How do I troubleshoot the result always returning "None'?
def render(source_html):
"""Fully render HTML, JavaScript and all."""
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Render(QWebEngineView):
def __init__(self, html):
self.html = None
self.app = QApplication(sys.argv)
QWebEngineView.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.setHtml(html)
self.app.exec_()
def _loadFinished(self, result):
# This is an async call, you need to wait for this
# to be called before closing the app
self.page().toHtml(self.callable)
def callable(self, data):
self.html = data
# Data has been stored, it's safe to quit the app
self.app.quit()
return Render(source_html).html
import requests
#url = 'http://webscraping.com'
#url='http://www.amazon.com'
url='https://www.ncbi.nlm.nih.gov/nuccore/CP002059.1'
sample_html = requests.get(url).text
print(sample_html)
print(render(sample_html))
EDIT: Thanks for the responses which were incorporated into the code. But now it returns an error and the script hangs until I kill the python launcher which then causes a segfault:
This is the revised code:
def render(source_url):
"""Fully render HTML, JavaScript and all."""
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Render(QWebEngineView):
def __init__(self, url):
self.html = None
self.app = QApplication(sys.argv)
QWebEngineView.__init__(self)
self.loadFinished.connect(self._loadFinished)
# self.setHtml(html)
self.load(QUrl(url))
self.app.exec_()
def _loadFinished(self, result):
# This is an async call, you need to wait for this
# to be called before closing the app
self.page().toHtml(self._callable)
def _callable(self, data):
self.html = data
# Data has been stored, it's safe to quit the app
self.app.quit()
return Render(source_url).html
# url = 'http://webscraping.com'
# url='http://www.amazon.com'
url = "https://www.ncbi.nlm.nih.gov/nuccore/CP002059.1"
print(render(url))
Which throws these errors:
$ python3 -tt fees-pkg-v2.py
Traceback (most recent call last):
File "fees-pkg-v2.py", line 30, in _callable
self.html = data
AttributeError: 'method' object has no attribute 'html'
None (hangs here until force-quit python launcher)
Segmentation fault: 11
$
I already started reading up on python classes to fully understand what I'm doing (always a good thing). I'm thinking something in my environment could be the problems (OSX Yosemite, Python 3.4.3, Qt5.4.1, sip-4.16.6). Any other suggestions?
The problem was the environment. I had manually installed Python 3.4.3, Qt5.4.1, and sip-4.16.6 and must have mucked something up. After installing Anaconda, the script started working. Thanks again.

Send data from javascript to python in google app engine

i want to create an application in google app engine using python. I have to send a list of json string to python script.I have done the following code but ain't worked.
$.post("/javascriptdata",{v:s},function(data,status) {});
In python script i have a class named javascriptdata to where data has to be send to
import wsgiref.handlers
import json
import sys
import cgi
from google.appengine.ext import webapp
from google.appengine.ext.webapp import Request
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
class mainh(webapp.RequestHandler):
def get(self):
self.response.out.write(template.render("paint.html",{}))
class javascriptdata(webapp.RequestHandler):
def post(self):
self.response.headers['content-Type'] = 'html'
data1=self.request.get('v')
self.response.out.write("""<html><body>""")
self.response.out.write(data1)
self.response.out.write("""</body></html>""")
def main():
app = webapp.WSGIApplication([
('/',mainh),("/save",javascriptdata)], debug=True)
wsgiref.handlers.CGIHandler().run(app)
if __name__ == "__main__":
main()
The javascriptdata is associated with the url "/save". I have created a submit button named "save" that would redirect to /save but iam not getting any output. I know it may be a silly mistake but Iam struggling to sort it out. Please suggest me how to post and read the data for this code.
This seems suspicious:
$.post("/javascriptdata",{v:s},function(data,status) {});
since you don't have a /javascriptdata URL mapped in the python code. Perhaps you meant
$.post('/save', ...?
Alternatively, you could change the WSGIApplication init to be:
...("/javascriptdata",javascriptdata)...

Flask, html and javascript desktop app

I want to build a desktop application using python, html and javascript. So far i have followed the tuts on flask and have a hello world working example. What should i do now to make it working? how do the html files "talk" to the python scripts below them?
here is my code so far :
from flask import Flask, url_for, render_template, redirect
app = Flask(__name__)
#app.route('/hello/')
#app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
#app.route('/')
def index():
return redirect(url_for('init'))
#app.route('/init/')
def init():
css = url_for('static', filename='zaab.css')
return render_template('init.html', csse=css)
if __name__ == '__main__':
app.run()
You can use HTML forms just as you normally would in your Jinja templates - then in your handler you use the following:
from flask import Flask, url_for, render_template, redirect
from flask import request # <-- add this
# ... snip setup code ...
# We need to specify the methods that we accept
#app.route("/test-post", methods=["GET","POST"])
def test_post():
# method tells us if the user submitted the form
if request.method == "POST":
name = request.form.name
email = request.form.email
return render_template("form_page.html", name=name, email=email)
If you wanted to use GET instaed of POST to submit the form you would just check request.args rather than request.form (See flask.Request's documentation for more information). If you are going to be doing much with forms though, I recommend checking out the excellent WTForms project and the Flask-WTForms extension.

Categories