I am new to API with Laravel. I am trying to validate parameters sent into POST request through the Laravel's validate method.
Following data is the only parameter I am sending through the POST request. I want to validate device_id for uniqueness using Laravel's validate method.
data => {"device_id":"xxxxxx2C-9EA3-4FFB-B26D-E8E483xxxxxx","password":"ANVp285x","user_type":"1"}
I can validate the only data parameter however there is no proper response or error message it's throwing exception. My main motive is to validate device_id, password and user_type.
Here's my controller's store
public function store(Request $request) {
try {
$result = $this->validate($request, [
'data' => 'required',
// 'data.device_id' => 'required|unique:users|max:60',
// 'data.password' => 'required|min:6|confirmed',
// 'data.user_type' => 'required',
]);
$user = new User;
// Parameters from request
$data = json_decode($request->data, TRUE);
$user->device_id = $data['device_id'];
$user->password = bcrypt($data['password']);
$user->u_type = $data['user_type'];
// Parameters to be generated
$user->qr_code_image = "test_qr_code.png";
$user->access_code = MyHelper::generate_access_code();
$user->save();
$user = User::find($user->id);
$result = Fractal::item($user, new UserTransformer)->getArray();
if (!empty($result['data'])) {
$result['status'] = TRUE;
$result['message'] = 'Success';
} else {
$result['status'] = FALSE;
$result['message'] = 'Failed';
}
} catch (\Exception $e) {
$result['status'] = FALSE;
$result['message'] = 'Failed';
}
return $result;
}
You can use Illuminate\Support\Facades\Validator to validate requests in your API:
$validator = Validator::make($request->all(), [
"device_id" => "required|unique:devices",
"user_type" => "required",
"password" => "required",
]);
if ($validator->fails()) {
return response(
$validator->errors(),
400
);
}
I found the solution I did this with below approch,
$data = $request->data;
$data = json_decode($data, TRUE);
$status = 200;
$validator = Validator::make(
array(
"goal_title" => $data['goal_name'],
"family_id" => $data['family_id'],
"goal_frequency" => $data['goal_frequency'],
), array(
"goal_title" => "required|regex:/(^[A-Za-z0-9 ]+$)+/|:family_goals",
"family_id" => "required",
"goal_frequency" => "required:goal_frequencies",
)
);
Related
This is my controller function to store a product, i have the error in the $validator, i'm using this in the api route, i have the error of the title, i've try so many things and nothing works, please helpme, if i send in $validator the $req->all() it works, but i need to send a picture and thats why i'm using the $productReq, i'm using laravel 8
public function store(Request $req)
{
$productReq = new Product($req->all());
if ($req->file('file') == null) {
$req->file = "";
} else {
$image = $req->file('file')->store('public/images');
$url = Storage::url($image);
$productReq->file = $url;
}
$rules = array(
'name' => 'required',
'price' => 'required',
'file' => 'required|image'
);
$validator = Validator::make($productReq, $rules);
if ($validator->fails()) {
return response()->json([
'error' => true,
'response' => $validator->errors()
], 401);
} else {
$product = Product::create($productReq);
return response()->json([
'error' => false,
'response' => $product,
], 200);
}
}
Validator::make() expects an array of data to be provided to it. You've provided an instance of a Product, which Laravel doesn't know what to do with. What you want to do is validate your data before creating an instance of Product.
public function store(Request $req)
{
$rules = array(
'name' => 'required',
'price' => 'required',
'file' => 'required|image'
);
$validator = Validator::make($req->input(), $rules);
if ($validator->fails()) {
return response()->json([
'error' => true,
'response' => $validator->errors()
], 401);
}
$product = new Product($req->input());
if ($req->file('file') == null) {
$req->file = "";
} else {
$image = $req->file('file')->store('public/images');
$url = Storage::url($image);
$product->file = $url;
}
$product->save();
return response()->json([
'error' => false,
'response' => $product,
], 200);
}
You can also simplify the controller's logic by making use of some of Laravel's conveniences. However, it may produce responses that do not match what the front end expects (i.e. JSON message when a validation error is encountered).
public function store(Request $req)
{
// Laravel's `validate()` method on a Request will validate against the
// current request data and return the valid input. It will throw an Exception
// if validation fails, which Laravel will handle and reply with the validation errors.
$validatedInput = $req->validate([
'name' => 'required',
'price' => 'required',
'file' => 'required|image'
])
$product = new Product($validatedInput);
// ... file logic
$product->save();
// In Laravel, you can return an array from a controller. Laravel
// will assume it's supposed to be JSON, and encode it automatically for you
return [
'error' => false,
'response' => $product,
];
}
I am developing my unit tests for an API created in Symfony4
Reading the Guzzle documentation I generated the following code:
File SecurityControllerTest.php
$client = new Client([
'base_uri' => 'http://localhost/sacrepad/sacrepad-api/public/index.php/',
'timeout' => 2.0,
]);
$data = array();
$data['email'] = 'admin#admin.com';
$data['password'] = '12345678';
$data2 = array();
$data2['json'] = $data;
$formData = json_encode($data);
$response = $client->request('POST', 'login', [
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
'form_params' => [
'json' => $formData,
]
]);
$body = json_decode($response->getBody(), true);
File SecurityController.php
/**
* #Route("/login", name="login", methods={"POST"})
*/
public function login(Request $request,Helpers $helpers,ValidatorInterface $validator, JwtAuth $jwtauth) {
$data = array(
'status' => 'error',
'code' => 400,
'msg' => 'data not received'
);
$json = $request->request->get('json');
$params = json_decode($json);
}
When I run the tests with the phpunit command, I get the following error:
1) App\Tests\SecurityControllerTest::testAuth GuzzleHttp\Exception\ServerException: Server error: `POST http://localhost/sacrepad/sacrepad-api/public/index.php/login` resulted in a `500 Internal Server Error` response:
If I change the name of the request:
$json = $request->request->get('json2');
It works and it returns me the following:
array(3) {
["status"]=>
string(5) "error"
["code"]=>
int(400)
["msg"]=>
string(18) "data not received"
}
Any ideas on how to make it work and send the parameters?
i build a class for working with guzzle
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class Api
{
protected $client;
protected $url;
public function __construct()
{
$this->client = new Client([
'verify'=>false
]);
$this->url = 'http://localhost/sacrepad/sacrepad-api/public/';
}
public function get($endpoint, $params = [], $headers = [])
{
$response = $this->sendRequest(
'GET',
$this->url . $endpoint,
$params,
$headers
);
return $response;
}
public function post($endpoint, $params = [], $headers = [])
{
$response = $this->sendRequest(
'POST',
$this->url . $endpoint,
$params,
$headers
);
return $response;
}
public function sendRequest($type, $url, $params = [], $headers = [])
{
if ($type == 'GET') {
$data = [
'query' => $params
];
} elseif ($type == 'FILE') {
$type = 'POST';
$data = [
'multipart' => $params // TODO implements later
];
} else {
$data = [
'json' => $params
];
}
if (!empty($headers)) {
$data['headers'] = $headers;
}
$data['headers']['X-REAL-IP'] = $_SERVER['REMOTE_ADDR'];
$data['headers']['User-Agent'] = $_SERVER['HTTP_USER_AGENT'];;
$data['headers']['X-Platform'] = 'web';
try {
$response = $this->client->request(
$type,
$url,
$data
);
if (in_array($response->getStatusCode(), ['200', '403', '404'])) {
return json_decode($response->getBody());
}
return false;
} catch (RequestException $re) {
if (in_array($re->getResponse()->getStatusCode(), ['403', '404', '422'])) {
return json_decode($re->getResponse()->getBody());
}
return json_decode($re->getResponse()->getBody());
} catch (Exception $e) {
return false;
}
}
}
when i want to send a post request it would be like this
$response = (new Api())->post('index.php/',[
'email'=> 'admin#admin.com',
'password' => '123456'
]);
now it will send a post request to index.php and send email and password data i hope it would be helpful
Here I have some slim PHP code which is log in and a function to check whether it decode the JWT that store in the header.
$app->post('/login', function ($request, $response) {
$input = $request->getParsedBody();
$settings = $this->get('settings'); // get settings array.
$sql = "SELECT id, password FROM users WHERE id= :id";
$sth = $this->db->prepare($sql);
$sth->bindParam("id", $input['id']);
$sth->execute();
$user = $sth->fetchObject();
// verify user id
if(!$user) {
return $this->response->withJson(['error' => true, 'message' => 'NO ID '], 404)->withHeader('Content-type', 'application/json;charset=utf-8', 404);
}
// Compare the input password and the password from database for a validation
if (strcmp($input['password'],$user->password)) {
return $this->response->withJson(['error' => true, 'message' => 'These credentials do not match our records.'], 404)->withHeader('Content-type', 'application/json;charset=utf-8', 404);
}
$payload = array(
"iat" => time(),
"exp" => time() + 36000,
// "id" => $input['id']
"context" => [
"user" => [
"id" => $input['id']
]
]
);
try {
$token = JWT::encode($payload, $settings['jwt']['secret'],"HS256"); // $token store the token of the user
} catch (Exception $e) {
echo json_encode($e);
}
return $this->response->withJson($payload,200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200)
->withAddedHeader('Authorization', $token);
});
$app->get('/get', function ($request, $response) {
$jwt = $request->getHeader("Authorization");
$settings = $this->get('settings');
$token = JWT::decode($jwt, $settings['jwt']['secret'], "HS256"); // $token store the token of the user
if ($token) {
return $this->response->withJson($token, 200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200);
}
return $this->response->withJson($token,401)
->withHeader('Content-type', 'application/json;charset=utf-8', 401);
});
But it when i try to run http://localhost:8080/get it returns an error which is
Argument 3 passed to Firebase\JWT\JWT::decode() must be of the type array.
Why does it happen and how can i fix it?
Try to follow what the error says:
$token = JWT::decode($jwt, $settings['jwt']['secret'], ["HS256"]);
You can see the Example of using here
If i decode in the same function it returns the decoded JWT, but if i decode in other function it returns an error. How to pass the jwt to other function?
$app->post('/login', function ($request, $response) {
$key = "supersecretkeyyoushouldnotcommittogithub";
$input = $request->getParsedBody();
$settings = $this->get('settings'); // get settings array.
$sql = "SELECT id, password FROM users WHERE id= :id";
$sth = $this->db->prepare($sql);
$sth->bindParam("id", $input['id']);
$sth->execute();
$user = $sth->fetchObject();
// verify user id
if(!$user) {
return $this->response->withJson(['error' => true, 'message' => 'NO ID '], 404)->withHeader('Content-type', 'application/json;charset=utf-8', 404);
}
// Compare the input password and the password from database for a validation
if (strcmp($input['password'],$user->password)) {
return $this->response->withJson(['error' => true, 'message' => 'These credentials do not match our records.'], 404)->withHeader('Content-type', 'application/json;charset=utf-8', 404);
}
$payload = array(
"iat" => time(),
"exp" => time() + 36000,
// "id" => $input['id']
"context" => [
"user" => [
"id" => $input['id']
]
]
);
try {
$token = JWT::encode($payload, $key); // $token store the token of the user
} catch (Exception $e) {
echo json_encode($e);
}
// return $this->response->withJson($payload,200)
// ->withHeader('Content-type', 'application/json;charset=utf-8', 200)
// ->withHeader('Authorization', $token);
$decoded = JWT::decode($token, $key, array('HS256'));
print_r($decoded);
});
I am trying to validate apikey , my scenario here is when am as a user enter apikey like xxxxxxxxxxxxxxxx-us17 i am saving this apikey in database as a user_id what i want here when i enter this key this key should check from mailchimp if its valid apikey that a user is enter if its a valid then good to go. if its not there should be a message not valid api key. On my side its allowing to enter any randon apikey which is not allowed in any case.
I am trying to solve this problem from a couple of days:
Here is my full controller:
namespace App\Http\Controllers;
use App\APIKEY;
use DrewM\MailChimp\MailChimp;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Mailchimp\MailchimpFacade;
class ApiController extends Controller
{
public function authenticateApiKey(Request $request)
{
$fieldsValidation = [
'api_key' => 'required|unique:apikey,api_key',
];
$validator = Validator::make($request->all(), $fieldsValidation);
if ($validator->fails()) {
$resultArray = [
'status' => 0,
'message' => $validator->errors()->first(),
'dataArray' => []
];
} else {
$api = new APIKEY($request->all());
$mailchimp_ping = new MailChimp($api);
print_r($mailchimp_ping);die;
$mailchimp_ping = $mailchimp_ping->get('/helper/ping');
if (!$mailchimp_ping) {
$errorResponse = [
'message' => 'Not valid api key!',
'error' => '401'
];
return \Illuminate\Support\Facades\Response::json($errorResponse);
}
else
{
$request->user()->apikey()->save($api);
$resultArray = ['status' => 1, 'message' => 'Mailchimp Api key added into system successfully!', 'dataArray' => $api];
return \Illuminate\Support\Facades\Response::json($resultArray, 200);
}
}
}
THe main code where i am trying to validate apikey from maiilchimp is:
$api = new APIKEY($request->all());
$mailchimp_ping = new MailChimp($api);
print_r($mailchimp_ping);die;
$mailchimp_ping = $mailchimp_ping->get('ping');
if (!$mailchimp_ping) {
$errorResponse = [
'message' => 'Not valid api key!',
'error' => '401'
];
return \Illuminate\Support\Facades\Response::json($errorResponse);
Your help will be highly appreciated !
I'm modifying the signup function that store user's data( email and password). I wish to include more data such as phone number, name and national ID. I tried but in mysql database it doesn't show anything. Here is what I have done :
public function signup(Request $request)
{
$credentials = $request->only([
'national_id' ,
'name' ,
'phone',
]);
$validator = Validator::make($credentials, [
'name' => 'required',
'email' => 'sometimes|email|unique:users',
'password' => 'sometimes|min:6|confirmed',
'password_confirmation' => 'sometimes|min:3',
'national_id' => 'required','unique:national_id',
]);
if ($validator->fails()) {
throw new ValidationHttpException($validator->errors()->all());
}
try {
$user = $this->users->create($request->except('roles', 'permissions'));
if (!$user->id) {
return $this->response->error('could_not_create_user', 500);
}
$hasToReleaseToken = Config::get('boilerplate.signup_token_release');
if ($hasToReleaseToken) {
$user->national_id = $request->national_id;
return $this->login($request);
}
return $this->response->created();
} catch (\Exception $e) {
return $this->response->error($e->getMessage(), 500);
}
}
when I dd($request->national_id it shows me the national_id data in postman, but in my database it's NULL.