Cakephp3 Cookie saves string not array - cakephp-3.0

I want to ask about Cakephp3 Cookie component.
Basicly, i have array:
$a = ['11','22'];
Now, i want to save this array to Cookie:
$this->Cookie->write('Ids',$a);
Now when i debug this cookie Ids, it returns srting, not array:
var_dump = string(9) "{["11","22"]}"
My question is, how can i write this values as array in cookie in Cakephp3.
Thank You.
I have tried to explode this strong, but a would like to do it in elegant way.

Related

Jmeter - How to read multiple values returned by GET request in the form of array in Json format and write it to a CSV file

Jmeter - I have multiple values ( 250 values approx as a minimum ) returned from a Get Request in the form of array in json format , i need pass each of the value in the array as a parameter in next GET request
Request 1
> Get http://xxx/store1?
Response in json format looks like this
{"store1":"peirre","inventorylist":["item1","item2","item3"..........."item250"]}
I will need to use each one of item in next GET request like this to get is feautures like price , available quantity,production site etc.,
Request 2
Get https://xxx/store1/item1?
Get https://xxx/store1/item2?
Get https://xxx/store1/item3?
It would be easy when i can read the response and write each value in the array to CSV file so that my next get request would simply read the CSV file and fire all the requests
Is there a way to achieve this ???
Thank you in advance
Add JSON Extractor as a child of the request which returns the above JSON
Configure it as follows:
Names of created variables: anything meaningful, i.e. item
JSON Path Expressions: $.inventorylist.*
Match No.: -1
Add ForEach Controller after the first HTTP Request sampler and configure it as follows:
Input variable prefix: whatever you used as "Names of created variables" in the JSON Extractor, i.e. item
Output variable name: anything meaningful, i.e. current_item
Add HTTP Request sampler as a child of the ForEach Controller and use https://xxx/store1/${current_item} in "Path" field - it will iterate through all the "item"s
Add a Json Extarctor to the first Get Request with the following properties
2.Add a JSR223 post processor and initialize a counter as shown below
Add a while controller and place your get request in while controller.
add the following condition
${__javaScript(parseInt(vars.get("counter"))<=parseInt(vars.get("List_matchNr")))}
as shown below
To your second get request add a JSR223 post processor and increment the counter as shown below
int counter = Integer.parseInt(vars.get("counter")) +1;
vars.put("counter",Integer.toString(counter));
Use ${__V(List_${counter})} to replace hardcoded value
This loop will run through the match number and sends a request with each item to server
For more information on while loop please follow the link

Query String spaces

I was wondering how to write a Query String in a POST HTTP form request if occur some space (I guess the problem would occur in case of GET form request too) .
I am going to make an example:
If I have for example a value "English (US)" would the Query String (for example) in a hypothetic log in form be:
username=value1&password=&language=English (US)
I mean would be language=English (US) correct with the space inside?
And one more question. Since I have to use "password", would the query string be sent like this "password=&"? Or must I write something between "password=" and "&"?
I am not sure why you are using Query string params on a POST. Usually you would just include all values in the post body. Also, it is not a good idea to send credentials as query parameters. An HTTP AUTH header is a better choice if you don't have a more secure option.
Nonetheless, if you use the JavaScript encodeURIComponent() function, it will properly encode the spaces.
If you are using jQuery $.ajax() calls on a GET and provide the values as the 'data' option, then jQuery will call encodeURIComponent for you.

Accessing data from API json response. Arrays? Laravel

I am trying to access the steamid data in a json response returned by an API, specifically the Steam API.
The responses look like this:
I've made it return json but why do I see array all over the place?
How would I access the steamid data? I'm getting a bit confused as I thought this would be json.
I'm using guzzle to get the data and converting it to json using the guzzle json() method:
Any help would be appreciated.
Thanks!
The API is indeed using JSON to send/receive , however JSON is just a string, so in order to use that data PHP must parse it, which is automatically handled by guzzle, so as soon as you get the data back it has automatically decoded the data into a usable format for yourself.
It does this using the json_encode() and json_decode() functions.
You'd be able to access the steamid with the following.
// Assuming $data is your response from the API.
$players = array_get($data, 'response.players', []);
foreach($players as $player)
{
$steamId = array_get($player, 'steamid', null);
}
Using the laravel helper array_get() function is a great way of ensuring you return a sane default if the data doesn't exist as well as eliminating the need to keep doing things like isset() to avoid errors about undefined indexes, etc. http://laravel.com/docs/5.1/helpers
Alternativly not using the laravel helpers you could use something similar to below, although I'd advise you add checks to avoid the aforementioned problems.
foreach($data['response']['players'] as $player)
{
$steamId = $player['steamid'];
}
If you didn't want guzzle to automatically decode the API's JSON I believe you should just be able to call the getBody() method to return the JSON string.
$json = $response->getBody();

ZF2: What's the easiest way to return a boolean json response?

In ZF1 it was pretty easy to send a json boolean response, for example, in the controller use:
return $this->_helper->json(true);
What's the easiest way to repeat this in ZF2?
I tried creating a new JsonModel with an array of variables. The only entry in the array was my boolean value (with a key of 0). This didn't work because the resolver was still off looking for a template.
I think I somehow need to return early?
EDIT:
I think this is a really important issue. For example, when the JQ Validation plugin uses a server-side validation method, it expects a JSON boolean response.
I managed to make my application JSON-ready by following 'alternate rendering and response strategies' section at the bottom of the Zend\View page, http://framework.zend.com/manual/2.0/en/modules/zend.view.quick-start.html. But this operates on the array that has been passed to the view, so the boolean true becomes json [true]
I tried the json view helper in various combinations, but couldn't get it to work.
Perhaps I need to create my own rendering and response strategies? That seems like overkill though...
Rob Allen has written an article about it:
Returning JSON from a ZF2 controller action
Also you can try this code to return every data without view rendering:
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent('some data');
return $response;
The easiest way:
echo "true";
exit;
Though you may want to output some appropriate headers.
Arguably the correct way would be to add the JsonStrategy as viewstrategy and use a JsonModel, but I think it always returns an object (the json_encoded associative array of view variables passed to the JsonModel).

Confused with JSON data and normal data in Django ajax request

I read about JSON from internet but still i have not got the grasp of it. I am reading this article
http://webcloud.se/log/AJAX-in-Django-with-jQuery/
I could not understood the first part where the function is using JSON
def xhr_test(request, format):
if request.is_ajax():
if format == 'xml':
mimetype = 'application/xml'
if format == 'json':
mimetype = 'application/javascript'
data = serializers.serialize(format, ExampleModel.objects.all())
return HttpResponse(data,mimetype)
# If you want to prevent non XHR calls
else:
return HttpResponse(status=400)
My Main Problems are
From where the function is getting format variable
Does format is json mean that data given to function is json or data which will be recived is json
Can anyone give me simple example that what will be the ouput of this function
data = serializers.serialize(format, ExampleModel.objects.all())
How will I use that data when i get that response in jquery function
If i don't use JSON in above function then how will the input and response back will chnage
Thanks
From where the function is getting format variable
In practice, there are lots of ways this format could be populated. HTTP provides an Accept: header that requests can use to indicate the preferred Content-Type for the response. On the client, you might use xhr.setRequestHeader('accept', 'application/json') to tell the server that you want your response in json format. In practice, though, very few frameworks actually do this. This being django, arguments to view functions are usually set in the urlconf, you might craft a urlconf like this:
urlpatterns = patterns('',
# ...
(r'^xhr_test.(?<format>.*)$', 'path.to.xhr_test'),
)
2 . Does format is json mean that data given to function is json or data which will be recived is json
This particular view doesn't do anything at all with the request body, and is certainly providing a response body in the supplied format
4 . How will I use that data when i get that response in jquery function
Depending on how complicated your request needs to be, you can use jQuery.getJSON, which will pass your callback with regular JavaScript objects that result from parsing the JSON. If you need to do a bit more work to get the request right, you can use jQuery.parseJSON to process the json data, and that will return the same JavaScript objects.
From the urlconf, just like it says in the article right below it.
The query set will be serialized as JSON.
It will be the query set represented as either XML or JSON. python manage.py shell is your friend.
You will decode it, then iterate over it.
You'll need to find some other format to serialize it in instead.