Like title states I am trying to use Restful services with the VPS that I have recently acquired.
I am using the IP address to the server along with the path to where to find the PHP script. When I use and test this in PostMan it works exactly like I want it to.
When I switch over to react-native in visual studio it continuously gives me a network request error.
If I remove the 's' in http's'://... I receive unrecognized token '<'.
I can navigate to the file in my browser and it tells me that the account was not created and I am missing the tokens Name, Email and Password. The file is their and is accessible publicly...
Here is the PHP code that is handling it..
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Connects us to the database
$conn = mysqli_connect('ip address', 'user', 'password');
$database = mysqli_select_db($conn, 'db');
// Get their unique ID
$UniqueID = uniqidReal();
// Collect the values that will be used
$MemberID = $UniqueID;
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$UserPass = password_hash($_POST['Password'], PASSWORD_DEFAULT);
// Make sure we have made a succesfull connection to the database
if ($conn->connect_error) {
die("Connection Failed: " . $conn->connect_error);
}
/**
* Generate the ID
*/
function uniqidReal($lenght = 13)
{
// uniqid gives 13 chars, but you could adjust it to your needs.
if (function_exists("random_bytes")) {
$bytes = random_bytes(ceil($lenght / 2));
} elseif (function_exists("openssl_random_pseudo_bytes")) {
$bytes = openssl_random_pseudo_bytes(ceil($lenght / 2));
} else {
throw new Exception("no cryptographically secure random function available");
}
return substr(bin2hex($bytes), 0, $lenght);
}
// prepare the query, bind the variable and execute
$stmt = $conn->prepare("SELECT COUNT(*) FROM member WHERE Email = ?");
$stmt->bind_param('s', $Email);
$stmt->execute();
// grab the result
$stmt->bind_result($numRows);
$stmt->fetch();
if ($numRows) {
echo "<p class='red'>Email is already registered with us</p>";
} else {
mysqli_stmt_reset ( $stmt );
//prepare and bind statement
$stmt = $conn->prepare("INSERT INTO member(MemberID, Name, Email, Password) VALUES(?,?,?,?)");
$stmt->bind_param("ssss", $MemberID, $Name, $Email, $UserPass);
echo ($conn->error);
// Execute the statement
$R = $stmt->execute();
if ($R) {
$Message = 'Account was created succesfully';
} else {
$Message = 'Warning Account not created';
}
}
echo ($Message);
Here is my fetch request from within React-Native.
// Database Connection
// Form Validation
const InsertRecord = () => {
console.log("Record Being Submitted!\n");
/**
* NAME VALIDATION BELOW
*/
if (name.length === 0) {
setNameCorrect(false);
} else {
setNameCorrect(true);
}
/**
* EMAIL VALIDATION BELOW
*/
let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w\w+)+$/;
if (reg.test(email) === false) {
setEmailCorrect(false);
} else {
setEmailCorrect(true);
}
/**
* PASSWORD VALIDATION
*/
if (password === repeatPassword && password.length !== 0) {
setPasswordCorrect(true);
} else {
setPasswordCorrect(false);
}
/**
* FINAL VALIDATION CHECK ON FORM
* If form isn't completed correctly following if statement will
* end the process not allowing it to continue for submission.
*/
if (!(passwordCorrect && nameCorrect && emailCorrect)) {
console.log("\nError form not filled out correctly!\n");
return false;
}
/**
* DATABASE API IS CALLED FOR SUBMISSION
*
*/
fetch("https://'ip address'/API/insert.php", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
Name: name,
Email: email,
Password: password,
}),
})
.then((response) => response.json())
.then((json) => {
return json;
})
.catch((error) => {
console.error(error);
});
};
I am really stuck and am unsure if their is a setting or a package I am missing to make this connection?
I am using the IDE Visual Studio and React-Native Expo... I understand with android their is a clear text traffic setting that I can alter but am unable to find it in React-Native unless I would have started with a bare project.
Below is the error code that I receive in console when sending the query request. 
source https://stackoverflow.com/questions/73533250/network-request-failed-trying-to-connect-to-vps-using-react-native-expo
No comments:
Post a Comment