Laravel - Modify Received Request Parameters - laravel

When we receive a Request object in Laravel, is there a way to modify or add data to it? For instance, could I rename a parameter (not the value, but the parameter name itself) to something else? For example, the input might be called fname but I want to change it to first_name. Or could I add new inputs and values that weren't in the original request?
The reason I ask is that I have a method that accepts a Request object, and expects certain input names. I'd like to be able to reuse the method, but the request input names will be different.

If you have an Object you can edit and add new items.
$request->url = $new_url;
$request->new_item = 1;
If the object item not exists, then will create automatically, or if it exists, will modify it.

Tested #marc-garcia answer, and that will not persist through your script execution. This will...
// merge defaults into the request.
// this makes it consistent everywhere (blade, controller...)
request()->merge([
// find the request if it exists, second param is the default value
'reservable' =>request( 'reservable', (self::RESERVABLE_BY_DEFAULT?1:0) )
]);
You may also use request()->replace([...]); but that will remove all other parameters from the request and replace it will the array you provide.

Related

Param with blank value triggers (!isset) validation in Laravel

Here is my script:-
if (!isset($request->security_token))
{
// Provide security token
$error = TRUE;
$err_message[] = Lang::get('caption_validation_error.ser_valid_security_token.value');
$err_validation[] = Lang::get('caption_validation_error.ser_valid_security_token.value').' [security_token]';
}
It means, if a param is not "sent", then the validation will be triggered. However, if a param with "blank" value is sent, it must not trigger the validation.
However, when I am hitting the api through POSTMAN app, the security_token with blank value enters the !isset validation.
What am I doing wrong?
This is not that obvious why it's like this because it seems quite strange at first glance. However if you know a bit more about Laravel and you look at app/Http/Kernel.php file you will see in there:
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
which as you can find out from name converts empty strings to null. So in your case when you are sending empty parameter it's considered as null so !isset($null) will be true.
So you have 2 options:
remove this middleware from array - however it will affect the whole application so it might not be the best way if you are not sure about it
Assuming you want to trigger this validation only if parameter is not sent at all instead of
if (!isset($request->security_token))
you can use for example
if (!$request->has('security_token'))
Obviously it's not exactly the same - if you now sent this token and set it to null it won't be still executed but I believe when you now know what's happening with this empty string you can now adjust it exactly to your needs.

How to get PUT and DELETE parameters from HTTPRequest in SilverStripe?

There are getVars(), postVars() and requestVars() methods in HTTPRequest.
They returns parameters from GET and POST requests.
But how can I get parameters from PUT and DELETE requests?
I know that there is a 'body' property somewhere in HTTPRequest. It contains PUT params in query string or something similar.
To use this property I need to parse 'body' field.
Is there a way to get these parameters with comfortable way, like for GET and POST?
You need to do it the old school way.
parse_str($request->getBody(), $params);
$foo = $params['Foo'];
Keep in mind the second parameter of parse_str is a reference, so no need to define that variable before invoking the function call.

How to use response field in a new request in Jmeter

I have this issue that I want to solve.
I want to create a new http request using field from previous response
I send a request
I used Json extractor to move the response string to a variable (let call this string nurl)
I used Regular expression and move the field that I want to "Reference Name"
(meanning from nurl I just want tt_cid)
Now I want to make a new call, and use that field tt_cid in my new call
How I shall call tt_cid? since it is not passed as User Defined Variables,
when I use tt_cid, I do not think J meter know it, since it is not written there, I just pulled it from the response.
Provided a Pic of what I have done
Regards to you all
Short answer call it ${tt_cid}.
since it is not passed as User Defined Variables, when I use tt_cid,
I do not think J meter know it,
For your understanding add Debug Sampler after Regular expression,
You will see all your JMeter variables, including tt_cid, which can be called as other variables ${tt_cid} inside other Samplers.
It's called Reference Name and not Variable Name because it's more complicated, You should read JMeter's Regular Expression to understand how it works internally, But basically it saves more than just 1 Variable.

How can I log the value of a variable sent in an HTTP Request sent from JMeter, if the value was first read in from a csv file

I would like to read the exact value of a variable I use to pass through an HTTP Request. I first read in many values of variables using the CSV Data Set Config. For the username, it is in the form of an email address. So, I have a variable called "email" in the Data Set Config. In the actual HTTP Request, for "name", I call it "username". For the "Value" field for this same "username", I added a time() function to it like this so I would end up creating unique users in my tests:
${email}${__time()
When I view the "Request" in a View Results Tree, I can see my parameter is listed correctly:
username=email1%40email.com1390854377360
I do not care if this is correct in real world terms. I already know that is not a valid email. That is ok for now.
What I want to know is how can I log that email that I just created on the fly? I would like to not have to pull in the whole request every time also and then use some type of Regular Expression extractor. It just seems like there should be an easy way to do this.
I think there are 2 ways,
Beanshell Pre/Post processors : you can write custom code in which you can log all your variables in some custom log file
Simple data writer : you can configure it and check save url,save field names,save response data field checkboxes that will give you complete data but in that later postprocessing on result file is required to get all usernames (email in your case).
first approach is easier and allows you create your own logging format(easy to retrieve and use somewhere else).
second approach is somewhat tedious and requires post processing.

What's the best way to pass additional POST variables into a validation rule in Kohana 3?

I'm trying to validate some POST data. One of the validations I need to do is a registration code, which is based off another POST variable - an IMEI number.
In my POST data I have 2 fields, register_imei and register_code. My code currently looks like this:
$post = Validate::factory($_POST);
$post->rule('register_imei', 'not_empty')
->rule('register_imei', 'exact_length', array(15))
->rule('register_imei', 'some_class::luhn_check');
$post->rule('register_code', 'not_empty')
->rule('register_code', 'some_class::valid_registration_code', array($_POST['register_imei']));
However, I'm not sure whether passing in the variable from the raw POST array field is ok, because it could be empty or not set. Does the fact that I've already added validation rules for register_imei above make it safe?
Does the fact that I've already added validation rules for register_imei above make it safe?
No validation is taken place until you call the check() method.
To solve your problem, use:
Arr::get($_POST, 'register_imei', NULL);
which returns the 3rd argument as a default if the key is not set in the array.

Resources