python - Error: The browser (or proxy) sent a request that this server could not understand
Get the solution ↓↓↓So I'm having some trouble with this code and I'm getting this error
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
So after checking everything is somewhere here in the HTML file I think
$("#button_confirm_transaction").click(function() {
$.ajax({
url: document.getElementById("node_url").value + "/transactions/new",
type: "POST",
header: {'Access-Control-Allow-Origin':'*'},
dataType: 'json',
data: $('#confirmation_transaction_form').serialize(),
success: function(response) {
//reset both forms
$("#transaction_form")[0].reset();
$("#confirmation_transaction_form")[0].reset();
//clean text boxes
$('#sender_public_key').val('');
$('#sender_private_key').val('');
$('#recipient_public_key').val('');
$('#amount').val('');
$('#basic_modal').modal('hide');
$('#success_transaction_modal').modal('show');
},
error: function(error) {
console.log(error);
}
})
});
or here in the python code itself
from collections import OrderedDict
from time import time
from flask import Flask, jsonify, request, render_template
from flask_cors import CORS
class Blockchain:
def __init__(self):
self.transactions = []
self.chain = []
# Create genesis block
self.create_block(0, '00')
def create_block(self, nonce, previous_hash):
"""
Add a block of transactions to the blockchain
"""
block = {'block_number': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.transactions,
'nonce': nonce,
'previous_hash': previous_hash}
# Reset the current list of transactions
self.transactions = []
self.chain.append(block)
def submit_transaction(self, sender_public_key, recipient_public_key, signature, amount):
"""
Add a transaction to transactions array if the signature verified
"""
# TODO: mining reward
# TODO: signature validation
transaction = OrderedDict({
'sender_public_key': sender_public_key,
'recipient_public_key': recipient_public_key,
'signature': signature,
'amount': amount
})
signature_verification = True
if signature_verification:
self.transactions.append(transaction)
return len(self.chain) + 1
else:
return False
blockchain = Blockchain()
app = Flask(__name__)
CORS(app)
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
values = request.form
# TODO: Check required fields
transaction_result = blockchain.submit_transaction(values['confirmation_sender_public_key'],
values['confirmation_recipient_public_key'],
values['transaction_signature'], values['confirmation_amount'])
if not transaction_result:
response = {'message': 'Invalid Transaction!'}
return jsonify(response), 406
else:
response = {'message': 'Transaction will be added to Block' + str(transaction_result)}
return jsonify(response), 201
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-p', '--port', default=5001, type=int, help='port to listen on')
args = parser.parse_args()
port = args.port
app.run(host='127.0.0.1', port=port)
So I'm getting that there was and error:
127.0.0.1 - - [29/Mar/2020 22:26:48] "POST /transactions/new HTTP/1.1" 400 -
from the PyCharm console log
Any ideas? I'm kind of new in python programming and I've been looking to fix this for 2 days now
Thank you in advance
Answer
Solution:
So after hours trying to figure out what was going on I've noticed that it was missing dedebug=True
at the end of the python code.
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-p', '--port', default=5001, type=int, help='port to listen on')
args = parser.parse_args()
port = args.port
app.run(host='127.0.0.1', port=port, debug=True)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the process class relies on proc_open, which is not available on your php installation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.