i try to build restAPI
when i check my code on postMan i got error that data incomplete
this is my code for create file
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once 'database.php';
include_once 'inquery.php';
$database = new Database();
$db = $database->getConnection();
$product = new Inquery($db);
$jsn = file_get_contents("php://input");
$data = json_decode($jsn);
if(
!empty($data->enqType) &&
!empty($data->title) &&
!empty($data->deptid) &&
!empty($data->enq_body)&&
!empty($data->taskPriority)
){
$t = time();
$product->enqType = $data->enqType;
$product->title = $data->title;
$product->deptid = $data->deptid;
$product->enq_body = $data->enq_body;
$product->taskPriority = $data->taskPriority;
$product->addedDate = $t;
$product->addedBy = 0;
if($product->create()){
http_response_code(201);
echo json_encode(array("message" => "Product was created."));
} else{
http_response_code(503);
echo json_encode(array("message" => "Unable to create product."));
}
}
else{
http_response_code(400);
echo json_encode(array("message" => "Unable to create product. Data is incomplete."));
}?>
this is my json file that contains what i need
{
"title" : "title1",
"enqType" : 1,
"deptid" : 3,
"enq_body" : "some text",
"taskPriority" : 1}
i try all things, i see that this code dosen't accept string
in my checks i recived this errors :
unexpected 's' in the image shared below:
Related
I am developing an app with Kotlin-PHP-MySql.
I will be appreciated if someone has an idea why I get this exception "com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path" when I call createUser function.
I don't get an exception when I call getUser function with Same api.
I checked these two functions in postman. JSONs are formatted properly. Begins with
{ "key" : "value", "key1" : "value1", .... }
This is my api:
interface Api {
...
#POST("/api/v1/users/register_user.php")
suspend fun createUser(
#Body userDto: UserInfoPreviewDto
) : QueryResponse
#POST("/api/v1/users/get_user.php")
suspend fun getUser(
#Body jsonObject : JsonObject
) : UserDto
}
and my Retrofit instance:
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(RamonApiUser::class.java)
and my server-side php code is
register_user.php file:
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-
Headers, Authorization, X-Requested-With");
include_once("../../config/database.php");
include_once("../../classes/user.php");
$db = new Database();
$connection = $db->connect();
$user = new User($connection);
if($_SERVER['REQUEST_METHOD'] === "POST") {
$data = json_decode(file_get_contents("php://input"));
$user->username = $data->username;
$user->password = $data->password;
$user->fullname = $data->fullname;
$user->email = $data->email;
if($user->register()) {
http_response_code(200);
$result = array(
"status" => "200",
"message" => "saved"
);
echo json_encode($result);
}
else {
http_response_code(500);
echo json_encode(array(
"status" => "500",
"message" => "Error 500"
));
}
}
else {
http_response_code(503);
echo json_encode(array(
"status" => "503",
"message" => "Error 503"
));
}
this my get_user.php file:
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers,
Authorization, X-Requested-With");
include_once("../../config/database.php");
include_once("../../classes/user.php");
$db = new Database();
$connection = $db->connect();
$user = new User($connection);
if($_SERVER['REQUEST_METHOD'] === "POST") {
$param = json_decode(file_get_contents("php://input"));
if(!empty($param->id)) {
$user->id = $param->id;
$user_data = $user->getById();
if (!empty($user_data)) {
http_response_code(200);
$user_data['isPublicProfile'] = (bool)$user_data['isPublicProfile'];
$user_data['isLastOnlineVisible'] =
(bool)$user_data['isLastOnlineVisible'];
$user_data['isPhoneNumberVisible'] =
(bool)$user_data['isPhoneNumberVisible'];
echo json_encode($user_data);
}
}
}
else {
http_response_code(503);
echo json_encode(array(
"status" => "0",
"message" => "503 error"
));
}
getById() function in user.php :
public function getById() {
$sqlQuery = "SELECT * FROM ". $this->table_name ." WHERE id = ? LIMIT 0,1";
$obj = $this->conn->prepare($sqlQuery);
$obj->bind_param("i", $this->id);
$obj->execute();
$data = $obj->get_result();
return $data->fetch_assoc();
}
I have QueryResponse data class in the client-side with the fields status and message. user->register() returns true or false . I think the problem is with my json_encode method. I also tried this:
I created a QueryReponse php class with fields status and message and encode this object like this:
$query_response = new QueryResponse();
$query_response->status = "200";
$query_response->message = "registration successful";
$echo json_encode($query_response);
it didn't help either.
I created another file called get_query_response.php. Just to make sure that my QueryResponse classes are OK and doesn't throw exceptions.
get_query_response.php:
....
include_once("../../config/database.php");
include_once("../../classes/query_response.php");
$db = new Database();
$connection = $db->connect();
$query_response = new QueryResponse($connection);
if($_SERVER['REQUEST_METHOD'] === "POST") {
$query_response->id = 1;
$query_response_data = $query_response->getById();
// I fetch query_response_data from mysql
if (!empty($query_response_data)) {
http_response_code(200);
$result = array(
"status" => "500",
"message" => "save successfully"
);
echo json_encode($result); // doesn't throw exception.
works fine
//echo json_encode($query_response_data);
//when i use this line, it doesn't throw exception
//either. works fine
}
}
I call this file get_query_response.php from my api like this:
interface MyApi {
...
#POST("/api/v1/users/get_query_response.php")
suspend fun getQueryResponse() : QueryResponse
...
}
No errors, no exceptions. Both echo json_encode($result) and echo json_encode($query_response_data) works fine.
The only difference between two register_user.php and get_query_response.php files is this line:
$data = json_decode(file_get_contents("php://input"));
I don't get the reason why these two files behave differently. They are almost identical. Why does not this code work in register_user.php?
$result = array(
"status" => "200",
"message" => "saved successfully"
);
echo json_encode($result);
I finally solved the problem. It was my data class UserInfo. One field of the class was being assigned null during instantiation. Api is sending a class which two properties of that class has null values. The problem is solved by assigning values to those properties.
I'm trying to access session variables in file2.php from file1.php, but I'm getting the following error: undefined variable in file2.php.
How can I fix this?
This is file1.php:
<?php
session_start();
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Origin, Authorization, X-Requested-With, Content-Type, Accept, x-xsrf-token");
include "config.php";
$postjson = json_decode(file_get_contents('php://input'), true);
$today = date('y-m-d H:i:s');
eif(isset($postjson['aks'])=="proses_login"){
$logindata = mysqli_fetch_array(mysqli_query($mysqli, "SELECT * FROM usuario
WHERE nomeUsuario='$postjson[nomeusuario]' AND senha='$postjson[Suasenha]'"));
if(isset($logindata['nomeUsuario'])==$postjson['nomeusuario'] AND ($logindata['senha'])==$postjson['Suasenha']){
$_SESSION['U']=$postjson['nomeusuario'];
$result = json_encode(array('success'=>true, 'msgs'=>'Bem Vindo '.$_SESSION['U']));
}else{
$result = json_encode(array('success'=>false, 'msgs'=>'Nome do Usuario ou Senha incorrecta'));
}
echo $result;
}
?>
and this is file2.php:
<?php
session_start();
header("Access-Control-Allow-Origin: *");
header('Content-Type: text/html; charset=utf-8');
$host = "mysql:host=localhost;dbname=eventosonline";
$usuario = "root";
$senha = "";
$con = new PDO($host, $usuario, $senha);
$sql = $con->prepare("SELECT * FROM usuario WHERE nomeUsuario ='".$_SESSION['U']." ' ");
$sql->execute();
I tried to include file1.php but it didn't work.
I am using localhost server with apache and mysql on xampp. I am implementing a login functionality for which php code is present in my api. When i make a POST request with POSTMAN result comes appropriately but when i make a ajax request i am getting 405 method not allowed error.
I think the headers are appropriately set but still i don't know why error is coming. I would highly appreciate if someone can point out why exactly is error coming and provide a solution code if possible.
this is my js code
```var sendRequest = function(formData) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
response = JSON.parse(this.responseText);
console.log("request sent succesfully..");
return response;
}
if (this.readyState == 4 && this.status == 401) {
response = false;
return response;
}
};
request.open("POST","../api/objects/user/login.php",true);
request.setRequestHeader("content-type", "application/json");
request.send(formData);
};```
this is my php code.
<?php
// required headers
header("Access-Control-Allow-Origin:
http://http://127.0.0.1:8080/login.html" );
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-
Headers, Authorization, X-Requested-With");
include_once '../../config/database.php';
include_once 'user.php';
//files for json web token
include_once '../../config/core.php';
include_once '../../libs/php-jwt-master/src/BeforeValidException.php';
include_once '../../libs/php-jwt-master/src/ExpiredException.php';
include_once '../../libs/php-jwt-
master/src/SignatureInvalidException.php';
include_once '../../libs/php-jwt-master/src/JWT.php';
use \Firebase\JWT\JWT;
$db = new Database();
$conn = $db->getConnection();
$user = new User($conn);
$data = json_decode(file_get_contents("php://input"));
$user->email = $data->email;
$email_exists = $user->emailExists();
if($email_exists && password_verify($data->password, $user->password))
{
$token = array(
"iss" => $iss,
"aud" => $aud,
"iat" => $iat,
"nbf" => $nbf,
"data" => array(
"id" => $user->id,
"firstname" => $user->firstname,
"lastname" => $user->lastname,
"email" => $user->email
)
);
http_response_code(200);
//generate JWT
$jwt = JWT::encode($token, $key);
echo json_encode(
array(
"message" => "Succesful Login",
"jwt" => $jwt
)
);
}
else {
http_response_code(401);
echo json_encode(array("message" => "Login failed"));
}
?>
postman result on sending email and password as json
{
"message": "Succesful Login",
"jwt":
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3QiLCJhdWQiOiJodHRwOlwvXC9sb2NhbGhvc3QiLCJpYXQiOjEzNTY5OTk1MjQsIm5iZiI6MTM1NzAwMDAwMCwiZGF0YSI6eyJpZCI6IjIiLCJmaXJzdG5hbWUiOiJwcmFuYXYiLCJsYXN0bmFtZSI6ImFyb3JhIiwiZW1haWwiOiJhcm9yYS5wcmFuYXYuMjAwMEBnbWFpbC5jb20ifX0.npQWi4HP_QlGmmlkaPve3MBdn8u_yD_MxplUSKebNOY"
}
error shown by browser
login.js:25 POST http://127.0.0.1:8080/api/objects/user/login.php 405 (Method Not Allowed)
So I'm trying to make an endpoint to access my database with and it works just fine in postman, but when calling the GET request on my website i get a CORS error:
Query for foreign site blocked: The same origin policy does not allow remote resource reading http://IPGOESHERE/cordova/endpoint/log.php?id=-1. (Cause: The CORS query failed).)
I've tried googling but was unable to find anything useful.
My server-sided code is in 2 files which i have included below:
models/Log.php:
class Log {
// database connection and table name
private $conn;
private $table_name = "logging";
// object properties
public $ID;
public $UserID;
public $Handling;
public $Date;
// constructor with $db as database connection
public function __construct($db) {
$this->conn = $db;
}
// read products
function read($id)
{
$query = "SELECT * FROM " . $this->table_name;
if ($id != '-1') {
// select query
$query .= " WHERE logging.ID = ".$id;
}
// prepare query statement
$stmt = $this->conn->prepare($query);
// execute query
$stmt->execute();
return $stmt;
}
}
log.php
// required headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 1000");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE");
header("Content-Type: application/json; charset=UTF-8");
// database connection will be here
include_once '../database.inc';
include_once '../models/Log.php';
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$id = $_GET['id'];
$database = new Database();
$db = $database->getConnection();
$Log = new Log($db);
// query products
$stmt = $Log->read($id);
$num = $stmt->rowCount();
// check if more than 0 record found
if ($num > 0) {
$products_arr = array();
$products_arr["records"] = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$product_item = array(
"ID" => $ID,
"UserID" => $UserID,
"Handling" => $Handling,
"Date" => $Date,
);
array_push($products_arr["records"], $product_item);
}
// set response code - 200 OK
http_response_code(200);
// show products data in json format
echo json_encode($products_arr);
} else {
// set response code - 404 Not found
http_response_code(404);
// tell the user no products found
echo json_encode(
array("message" => "No log found")
);
}
}
Turns out it was due to php not allowing me to use the class name "Log" renamed everything called log & it now works flawlessly.
I am developing a hybrid application using Ionic Framework. I am using $http Restful web service to POST data into my server and get the response from server. When I test it in browser, it works well and I able to retrieve data from serve. But when I test it in my android device, it does not work at all. The following is my code.
Angularjs Code
$scope.validateBorrower = function(){
employeeNumber = $scope.assetBorrowing.employeeNumber;
employeeName = $scope.assetBorrowing.employeeName;
employeeDeparment = $scope.assetBorrowing.employeeDepartment;
if(employeeNumber == null && employeeName == null){
alert("Please Enter Employee Number or Employee Name");
}else{
$http({
url: "http://131.4.44.69/php3/validateBorrower.php",
method: "POST",
data: {
'employeeNumber': employeeNumber,
'employeeName' : employeeName
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(response) {
alert("Valid Employee");
$scope.assetBorrowing.employeeNumber = response[0].employeeNumber;
$scope.assetBorrowing.employeeName = response[0].employeeName;
$scope.assetBorrowing.employeeDepartment = response[0].employeeDepartment;
$scope.borrowbtn = false;
}).error(function(response) {
alert("error");
});
alert(employeeNumber);
}
}
PHP Backend Code
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: false");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
header('P3P: CP="CAO PSA OUR"');
header("Content-Type: application/json; charset=utf-8");
$postdata = file_get_contents("php://input");
$employeeRequest = json_decode($postdata, true);
#$employeeNumber = $employeeRequest['employeeNumber'];
#$employeeName = $employeeRequest['employeeName'];
if(empty($employeeNumber)){
#$employeeName = $employeeRequest['employeeName'];
}else{
#$employeeName = "";
}
$db_host = "localhost";
$db_user = "jack";
$db_password = "1234";
$db_database = "daikin_asset_management";
$conn = new mysqli($db_host, $db_user, $db_password, $db_database);
if($conn->connect_error){
echo "Connection Problem";
}
if(empty($employeeNumber) && empty($employeeName)){
echo "Error";
}else{
$getUserQuery = "SELECT * FROM xpouser WHERE (username='$employeeName') OR (employee_no = '$employeeNumber') ";
$getUserRetval = $conn->query($getUserQuery);
$userRow = mysqli_fetch_array($getUserRetval);
$employeeName = $userRow['username'];
$employeeNumber = $userRow['employee_no'];
$employeeDepartment = $userRow['department'];
$_arr = array();
$array = array(
'employeeNumber' => $employeeNumber,
'employeeName' => $employeeName,
'employeeDepartment' => $employeeDepartment
);
$jsonObjectChannel = json_encode($array);
$jsonObjectChannel = "[" . $jsonObjectChannel . "]";
echo $jsonObjectChannel;
}
?>
I figure it out by myself. Is because of I never add few permissions in my android Manifest file. I have to include this 3 permissions in my android manifest.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Next, I have to include cordova-whitelist plugin and add this into my config.xml
<access origin = "http://example.com" />
<allow-navigation href = "*://*.example.com/*" />