Is it possible to POST associations using "finder" URIs in Spring Data REST? - spring-data-rest

I would like to POST to create entities using associations that specify "finder" URIs rather than "identity" URIs. Is this possible?
POST this:
{
"username": "gonzo",
"role": "http://x.com/roles/nameequals?name=admin"
}
NOT this:
{
"username": "gonzo",
"role": "http://x.com/roles/1"
}
When I try this I get an error from Spring REST "failed to convert from type java.lang.String to type java.lang.Long for value 'name'" which I guess means it's not supported. I was hoping that there was some way of enabling this feature. Though that's probably wishful thinking.
Cheers

No, that's not supported. The URI needs to identify an item resource as we extract the backend ID from the URI directly to invoke findOne(…) on the repository.

Related

Override default JsonSerializer on serialize exception

Is it possible, to override default Net.Core JsonSerializer or catch Exception on serialize?
Actually, when i pass wrong field type to model, required in controler(for example property is type of string i will pass int) then response look like this
{
"status": 400,
"message": "Validation error",
"data": [ {
"filedName": "$.Username",
"validateErrors": ["The JSON value could not be converted to System.String. Path: $.Username | LineNumber: 0 | BytePositionInLine: 16."]
}]
}
when i print this message to user, he won't know what i'm talking about. I need to ovveride this message for "Value is incorrect. Required [type]"
I have custom Provider which implement IValidationMetadataProvider to customize response on model validation error, but those error is not catching there.
You have three options
Catch the Json exception in server middleware, interrogate it, and then convert it to something more succint for the client. Search up "app.UseMiddleware<>()".
Inspect the status code of the HttpResponse message on the client, interrogate it, and then convert it to something more succinct for the UI.
Perform rudimentary format / variable type validations on the client before sending to the controller / API.
I would recommend approach 3. The API has presented a contract to you for the payload. The least the client / UI can do is make sure it complies with the contract. Failures due to business logic / rules definitely do belong on the server.

How do we filter the entities which is not start with "msdn" using MSDynamics Web API

I want to get all entities which are the name not start the prefix as 'msdn' from ms dynamics.
I tried the below APIs, got the error.
GET /api/data/v9.1/EntityDefinitions?$select=LogicalName&$filter=not startswith(LogicalName,%27msdn%27)
Response :
{
"error":
{"code":"0x0",
"message":"The \"startswith\" function isn't supported for Metadata Entities."
}
}
I referred https://learn.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/query-data-web-api#standard-query-functions
I have checked that in one of my environment as well. What you require is not possible.
You will have to go 2 steps.
Retrieve all entities and then filter them out in your local program may it be JavaScript/C# or Json filtering/Power automate or something.

Wit AI response for API requests

I'm using wit ai for a bot and I think it's amazing. However, I must provide the customer with screens in my web app to train and manage the app. And here I found a big problem (or maybe I'm just lost). The documentation of the REST API is not enough to design a client that acts like the wit console (not even close). it's like a tutorial of what endpoints you can hit and an overview of the parameters, but no clean explanation of the structure of the response.
For example, there is no endpoint to get the insights edge. Also and most importantly, no clear documentation about the response structure when hitting the message endpoints (i.e. the structure the returned entities: are they prebuilt or not, and if they are, is the value a string or an object or array, and what the object might contain [e.g. datetime]). Also the problem of the deprecated guide and the new guide (the new guide should be done and complete by now). I'm building parts of the code based on my testing. Sometimes when I test something new (like adding a range in the datetime entity instead of just a value), I get an error when I try to set the values to the user since I haven't parsed the response right, and the new info I get makes me modify the DB structure at my end sometimes.
So, the bottom line, is there a complete reference that I can implement a complete client in my web app (my web app is in Java by the way and I couldn't find a client library that handles the latest version of the API)? Again, the tool is AWESOME but the documentation is not enough, or maybe I'm missing something.
The document is not enough of course but I think its pretty straightforward. And from what I read there is response structure under "Return the meaning of a sentence".
It's response in JSON format. So you need to decode the response first.
Example Request:
$ curl -XGET 'https://api.wit.ai/message?v=20170307&q=how%20many%20people%20between%20Tuesday%20and%20Friday' \
-H 'Authorization: Bearer $TOKEN'
Example Response:
{
"msg_id": "387b8515-0c1d-42a9-aa80-e68b66b66c27",
"_text": "how many people between Tuesday and Friday",
"entities": {
"metric": [ {
"metadata": "{'code': 324}",
"value": "metric_visitor",
"confidence": 0.9231
} ],
"datetime": [ {
"value": {
"from": "2014-07-01T00:00:00.000-07:00",
"to": "2014-07-02T00:00:00.000-07:00"
},
"confidence": 1
}, {
"value": {
"from": "2014-07-04T00:00:00.000-07:00",
"to": "2014-07-05T00:00:00.000-07:00"
},
"confidence": 1
} ]
}
}
You can read more about response structure under Return the meaning of a sentence

How to get AWS API Gateway to validate JSON Body against Model

The newly released "x-amazon-apigateway-request-validator" (in April I believe) is supposed to be able to run a JSON schema validation against the POST/PUT payload of an API.
I've tried to implement this in several different ways now but I can't make it work...
I have added the validators:
"x-amazon-apigateway-request-validators": {
"ValidateBody": {
"validateRequestParameters": false,
"validateRequestBody": true
},
"ValidateHeaders": {
"validateRequestParameters": true,
"validateRequestBody": false
}
}
For any methind I will look for specific headers so I also have the parameters in the "root" of the Swagger:
"x-amazon-apigateway-request-validator": "ValidateHeaders"
In the POST I have a x-amazon-apigateway-request-validator: ValidateBody and then a Model with the JSON schema where several elements are required.
I can however POST anything as long as it is JSON... There is no validation against the JSON schema done...
What am I missing?
It is working!
There is a time lag between the deploy and the actual usage of the new code even though I am not doing any caching it seems.
Went out for lunch and came back to a working solution!
{
"message": "Invalid request body"
}
It would be nice to be able to modify the response message though...

How to set filename in $_FILES global from Mirth Connect

I'm uploading a PDF using Mirth Connect. I've set the destination to a HTTP sender. The Content type is multipart/formdata and the body is just $(message.rawData).
I'm using a debugger and I can see the request successfully coming in. However the $_FILES array is structured like this:
{
"e00ce2d3-9bc4-4ce4-a923-2f5aecb1e5fc7756866505906950154_tmp": {
"name": "e00ce2d3-9bc4-4ce4-a923-2f5aecb1e5fc7756866505906950154.tmp",
"type": "multipart/form-data",
"tmp_name": "/tmp/phpJhm8Bf",
"error": 0,
"size": 32840
}
}
My problem is that I would like to use the original file name as the key for the $_FILES array. Is this possible using Mirth, or maybe is that name an encoded version of the original?
I know there are ways around this, I could add the original name as query parameter, but just out of curiosity I would like to know if it's possible to change the name of the key of the file upload.
If you're using File connector, then you can use the "originalFilename" source map variable. you can just reference it like this:
${originalFilename}