I have a python flask web service. which uses app = Flask(name)
CORS(app) # Enable CORS for all routes
In postman I set http Post
myUrl
Body
{
"username": "testuser",
"password": "securepassword123"
}
This works and inserts into the mysql table.
In TMSWeb core I have:
procedure TForm1.WebButton1Click(Sender: TObject);
var
JsonToSend: string;
begin
JsonToSend := '{"username":"testuser", "password":"testpassword"}';
WebHttpRequestRegister.Command := httpPOST; // Set the request method to POST
// Set the request headers
WebHttpRequestRegister.Headers.Clear;
WebHttpRequestRegister.Headers.Add('Content-Type: application/json');
// Set the request body
WebHttpRequestRegister.PostData := JsonToSend;
// Execute the request
WebHttpRequestRegister.Execute();//
end;
In the console I get this error
WEBLib.REST.pas:407 Uncaught DOMException: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': '' is not a valid HTTP header field name.
at Object.Execute$2 (http://localhost:8000/hseasy/hseasy.js:29908:13)
at Object.Execute (http://localhost:8000/hseasy/hseasy.js:29870:12)
at Object.WebButton1Click (http://localhost:8000/hseasy/hseasy.js:62173:35)
at Object.cb [as FOnClick] (http://localhost:8000/hseasy/hseasy.js:245:19)
at Object.Click (http://localhost:8000/hseasy/hseasy.js:35106:61)
at Object.Click (http://localhost:8000/hseasy/hseasy.js:48347:45)
at Object.HandleDoClick (http://localhost:8000/hseasy/hseasy.js:34585:31)
at HTMLButtonElement.cb (http://localhost:8000/hseasy/hseasy.js:241:26)
What am I doing wrong please.
Is this with the latest release TMS WEB Core v2.5.2.0?
What do you see as header that was sent in the browser console under the networking tab?
I have upgraded to TMS WEB Core v2.5.2.0
I don't see any related to the URL ??
You need to use:
WebHttpRequestRegister.Headers.AddPair('Content-Type','application/json');
In Postman this works
https://myURL.com/pythonScript/register
Body = {
"username": "myUser3",
"password": "myPassword3"
}
Header Content-Type = application/json
My code
procedure TForm1.WebButton1Click(Sender: TObject);
var
JsonToSend: string;
begin
JsonToSend := '{"username": "myUser","password": "myPassword"} ';
WebHttpRequestRegister.Command := httpPOST; // Set the request method to POST
// Set the request headers
WebHttpRequestRegister.Headers.Clear;
WebHttpRequestRegister.Headers.AddPair('Content-Type','application/json');
// Set the request body
WebHttpRequestRegister.PostData := JsonToSend;
// Execute the request
WebHttpRequestRegister.Execute();//
//
end;
procedure TForm1.WebHttpRequestRegisterRequestResponse(Sender: TObject;
ARequest: TJSXMLHttpRequestRecord; AResponse: string);
var
begin
showmessage(AResponse);
end
Am I sending the json in the body here ?
Console
XHR failed loading: POST "https://myUrl.com/pythonScript/register".
Python server code
@app.route('/register', methods=['POST'])
def register():
data = request.json
user_username = data['username']
user_password = data['password']
# Encoding the password using SHA-256
encoded_password = sha256(user_password.encode()).hexdigest()
# Insert the user into the database or return error message if email already exists
result = insert_user(user_username, encoded_password)
if result == True:
return jsonify({"success": True, "message": "User registered successfully!"}), 200
else:
return jsonify({"success": False, "message": result}), 400
XHR failed looks like a CORS error from the server.
Try to CORS enable your python server
I now have on the server:
from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import asc
from collections import OrderedDict
import os
from dotenv import load_dotenv
import mysql.connector
from hashlib import sha256
from flask_cors import CORS
import logging
# Load environment variables from a .env file if present
load_dotenv()
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
if I use
procedure TForm1.WebHttpRequestRegisterError(Sender: TObject;
ARequest: TJSXMLHttpRequestRecord; Event: TJSEventRecord;
var Handled: Boolean);
begin
ShowMessage('Error: ' + ARequest.responseText); // Displays the error response from the server.
end;
I get [Error] Unit1.pas(434): identifier not found "responseText"
So I can't trace the error
Because there is no property responseText
So, you need to use ARequest.req.responseText
Thanks got that working.....