CakePHP3.4: How to send a json object response? - cakephp-3.4

I try to migrate to 3.4 and I have a problem to send a json object.
Up to 3.3, I used the following code:
$jsonSites = json_encode([
'reqLocation' => [
'latitude' => $latitude,
'longitude' => $longitude
],
'sites' => $sitesList,
'discoveryBooks' => $discoveryBooksList,
'deleteSites' => !empty($inDeviceSites) ? [$inDeviceSites] : [],
'deleteBooks' => !empty($inDeviceBooks) ? [$inDeviceBooks] : []
]);
$this->response->type('application/json');
$this->response->body($jsonSites);
And my client received such kind of object:
{
"reqLocation": {
"latitude": 48.080563,
"longitude": 4.4649
},
"sites": [
{
"id": 5076,
"name": "...",
"modified": "2017-01-28T03:03:23+00:00",
"directory_name": "fr/26/26120_56cc30ea4d907",
"type": "portail",
"longitude": 7.031953,
"latitude": 47.939468,
"image_0": "jpg",
"picto_color": "#FFDDDDDD",
"agthemes": [],
"distance": 131.29188575851,
"category": 1281,
"category_name": "Jardin",
"sitecategories": [
{
"id": 10,
"code": 1281,
"name_fr": "Jardin",
"_joinData": {
"id": 1876,
"site_id": 5076,
"site_category_id": 10,
"authorized": true
}
},
{
"id": 33,
"code": 1283,
"name_fr": "Jardin botanique",
"_joinData": {
"id": 5693,
"site_id": 5076,
"site_category_id": 33,
"authorized": true
}
}
]
},
],
"discoveryBooks": [],
"deleteSites": [],
"deleteBooks": []
}
So now I just replaced the deprecated Response methods like that:
EDIT: Of course I return the response object but that cannot work like done here!
$this->response->withType('application/json');
$this->response->withStringBody($jsonSites);
return $this->response;
But now my client doesn't receive anything. I also tried with postman, nothing!
What's wrong?

Check the migration guide, the new reponse methods follow the PSR-7 immutability pattern.
Request & Response Deprecations
The bulk of deprecations for 3.4 are in the Request and Response
objects. The existing methods that modify objects in-place are now
deprecated, and superseded by methods that follow the immutable object
patterns described in the PSR-7 standard.
Cookbook > 3.x Migration Guide > 3.4 Migration Guide > Request & Response Deprecations
Adopting Immutable Responses
Before you migrate your code to use the new response methods you
should be aware of the conceptual differences the new methods have.
The immutable methods are generally indicated using a with prefix. For
example, withLocation(). Because these methods operate in an immutable
context, they return new instances which you need to assign to
variables or properties. If you had controller code that looked like:
$response = $this->response;
$response->location('/login')
$response->header('X-something', 'a value');
If you were to simply find & replace method names your code would break.
Instead you must now use code that looks like:
$this->response = $this->response
->withLocation('/login')
->withHeader('X-something', 'a value');
There are a few key differences:
The result of your changes is re-assigned to $this->response. This is
critical to preserving the intent of the above code. The setter
methods can all be chained together. This allows you to skip storing
all the intermediate objects.
Cookbook > 3.x Migration Guide > 3.4 Migration Guide > Adopting Immutable Responses
Long story short, in your case you must return the new request object created by the immutable methods:
return $this->response
->withType('application/json');
->withStringBody($jsonSites);
If you wouldn't return a response object, then you'd need to reassign the new reponse to $this->response as mentioned in the above quote.

Related

how to specify certain field which is inside array in restapi

I have an api link https://apilink.com?_fields=id,name,images which gives me the following format
[
{
"id": 229210,
"name": "Basic Electrical Knowledge",
"images": [
{
"id": 229211,
"date_created": "2023-01-13T18:34:39",
"date_created_gmt": "2023-01-13T07:34:39",
"date_modified": "2023-01-13T18:34:39",
"date_modified_gmt": "2023-01-13T07:34:39",
"src": "https://sampleSite.in/wp-content/uploads/2023/01/SomeUrlSource.jpg",
"name": "Basic Electrical Knowledge",
"alt": ""
}
]
}
]
I want to access only src from images[]. How do I retrieve this from the link. When clicking the link I want to display this:
[
{
"id": 229210,
"name": "Basic Electrical Knowledge",
"src": "https://sampleSite.in/wp-content/uploads/2023/01/SomeUrlSource.jpg"
}
]
How do I do this?
I tried to solve this by providing this parameters:
https://apilink.com?_fields=id,name,images=src
You can achieve this by making a GET request to the API link and then using a library such as JSON.parse() to parse the response and extract the necessary data. After that, you can use a for loop to iterate over the 'images' array in the response and extract the 'src' key from each object in the array. Finally, you can construct a new object with the desired format and return it.
fetch(https://apilink.com?_fields=id,name,images)
.then(response => response.json())
.then(data => {
let newData = []
data.forEach(item => {
let newItem = {
id: item.id,
name: item.name,
src: item.images[0].src
}
newData.push(newItem)
});
return newData;
})
.then(newData => {
console.log(newData);
});
Note that this code snippet is simplified and doesn't handle errors, it's only serve as an example of how you could do it.
Assuming that you can't make server-side changes, implement a little script, and want the result just manipulating the URI the response is no.
The URI is referring to a resource in the server, the _fields seem like a projection to make to the attributes of the desired resource.
In this case, you are trying to make a transformation on the resource given by the server through. If the server does not implement such functionality you must do it by yourself.
You want to transform the attribute images that has type [Object] to a String.
A code snippet like the answered by #RASIKA EKANAYAKA would fit your requirement.

Shopware 6 Admin Api - Updating existing record through patch method. Not working

shopware 6 admin api patch - why it's failing? I get error "Only single write operations are supported"
Following is api for rule-condition entity in the database, I update it with Id.
For same api get method is working!
url: api/rule-condition/dbb0d904c7c14860a9a94cf26b94eca6
method: patch
json body
[
{
"op": "replace",
"path": "/data/attributes/value/email",
"value": "test#gmail.com"
}
]
response:
{
"errors": [
{
"code": "0",
"status": "400",
"title": "Bad Request",
"detail": "Only single write operations are supported. Please send the entities one by one or use the /sync api endpoint.",
.......
I also tried changing json body to following
{
"data": {
"attributes": {
"value": {
"email": "test#gmail.com"
}
}
} }
Still it's not updating. Can somebody check and let me know what am i missing?
Documentation I followed:
https://shopware.stoplight.io/docs/admin-api/ZG9jOjEyMzA4NTQ5-writing-entities
This website has all apis and example methods. https://swagger.docs.fos.gg/,
rule-condition entity can also be found there.
Btw : I used postman for testing api
You're passing an array of objects in the request body, suggesting you want to update multiple records, but the endpoint only supports updating a single record. The correct payload in your case should look like this:
{
"value": {
"operator": "=",
"email": "test#gmail.com"
}
}
Notice that value is a json field and not only includes a single value. The exact content and the names of the properties of value depend on the type of condition used and usually it also includes the operator used in the condition.

How to pass multiple parameters in post request in karate framework?

I have a JSON data file which has data like
{
"Status": "Pending",
"role": "manager",
"client": "android",
"user": "test#abc.com",
"eTyres":
{
"Wheels": {
"title": "Alloy Wheel",
"value": "Yes"
}
}
}
Firstly, I want to read this data and when Wheels.value == Yes then I want to hit an API else hit another API
Also, I would like to know how i can pass multiple parameters in post request or from the file.
Post request data is as follows:
title:Alloy_wheel__Info
part:acCooling
partTitle:AC Cooling
partValue:No
Above data i'm passing through "form-data" in postman.
Thanks in advance
Your question is hard to understand and I will assume that you want to loop over some given JSON array and do some actions. Sounds like you are not "testing" and mis-using Karate !
To loop over a JSON array, use call. Refer the docs: https://github.com/intuit/karate#data-driven-features
To do conditionals, read this part of the documentation: https://github.com/intuit/karate#conditional-logic
To do "form-data" read this: https://github.com/intuit/karate#form-field
* def data = { "Status": "Pending", "role": "manager", "client": "android", "user": "test#abc.com", "eTyres": { "Wheels": { "title": "Alloy Wheel", "value": "Yes" } } }
* eval data.eTyres.Wheels.value == 'Yes' ? karate.call('api1.feature') : karate.call('api2.feature')
How to implement api1.feature and api2.feature is a homework for you. Keep in mind that in both you will still have access to the data variable. And please read the docs and examples !

Why to use actions in Dialogflow

I am new to Dialogflow so my question may be too simple. However, I do not understand what is the purpose of naming actions in Dialogflow. I have watched videos on youtube and people in them are using actions when they have a webhook. For example they may have an if condition in their source code
(e.g. in python
if action == 'action_name':
...
)
which executes something particular in this case.
However the json output which is retrieved by the source code has the following form:
{
"id": "123d9e8e-314f-451b-8b15-5e3b55baa980",
"timestamp": "2018-03-16T17:03:05.987Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "Hello",
"action": "input.welcome",
"actionIncomplete": false,
"parameters": {},
"contexts": [],
"metadata": {
"intentId": "effe6b2b-3372-4f89-882f-ff937b2b2abb",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "Welcome"
},
"fulfillment": {
"speech": "Hello, how can I help you?",
"messages": [
{
"type": 0,
"speech": "Hello, how can I help you?"
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"sessionId": "491d57cb-0af2-45ac-a658-9e47ec6658ce",
"alternativeResultsFromKnowledgeService": {}
}
Since the json data contains the IntentName why to bother naming an unique action for this specific intent when you can get directly the name of the intent in your json?
I tend to think of this in two ways, depending on exactly what I'm building. (Or sometimes a combination of these two ways.)
The Intent Name is a human-usable name, while the Action is something that is more intended for use by the webhook and more directly maps to a function.
Since you can have more than one Intent use the same Action, it can be convenient to map a few different ways the user may say something (and the parameters they may send along with them) to the same method. While you could do that by listing all the different Intent names in your code, it is easier to do that on the Dialogflow side.
In truth - use whatever works best for you. I tend to name my Intents and my Actions very similarly, but do branching based on what makes the most sense for the code (which sometimes also includes other values that may be sent).

How to post event with metadata to stream through HTTP API

I'm using EventStore and want to post a message (event) to it. I use the HTTP API for testing purposes. I've managed to post the event itself, with an event type specified, but I can't figure out how to specify metadata for my event. (and I must provide this metadata because my consuming application on the other side expects it).
This is what my HTTP request looks like:
Content-Type: application/json
ES-EventType: My.own.event.type
POST http://10.0.75.2:2113/web/index.html#/streams/foobar
{
"props": "andvalues"
}
Do I specify metadata in the body in through headers? I can't find much docs about this, only the official that doesn't mention it.
The documentation mentions the full schema for an event being written. It looks like this:
[
{
"eventId" : "string",
"eventType" : "string",
"data" : "object",
"metadata" : "object"
}
]
For example:
[
{
"eventId": "fbf4a1a1-b4a3-4dfe-a01f-ec52c34e16e4",
"eventType": "event-type",
"data": { "a": "1" },
"metadata": { "b": "2" }
}
]
Note that it's an array, and that you must pass content-type as application/vnd.eventstore.events+json
Check this page, scroll to Event Store Events Media Type.