how to tell rest post data is invalid in spring - spring

I have a post data request as the following,
{
"doctorId":1,
"userId":29,
"sampleConditions":[
{
"conditionId":"14",
"conditionName":"Acne",
"conditionDate":"2017-01-31"
}
],
"labOrder":{
"testId" : [1,2,3]
}
}
what happens, if the client gives the incorrect key
"sampleConditionss":[
{
"conditionId":"14",
"conditionName":"Acne",
"conditionDate":"2017-01-31"
}
],
Instead of giving sampleConditions, the client have sent the request as sampleConditionss.
How can i handle the request,

When you are using springs the spring framework will return 400 and you need not do anything here

Related

Aws-appSync Resolver mapping templte getting sliced data

Currently my data return from graphql query is in format and have 1200 questions from my http data source and want to return only first 10 through graphl in response mapping template.
{
"data": {
"questions":[
{
questions:"ques 1"
},
{
questions:"ques 2"
}, ....
]
}
}
My response mapping template code look likes this for slicing on aws appsycn(VTL direct resolver)
#if($ctx.result.statusCode == 200)
$ctx.result.body.data.questions.slice(0,10)
But getting error message:
Unable to convert $ctx.result.body.data.questions.slice(0,10)\n to Object"
i have tries to return via $util.toJson() also, but still facing same error.

Postman test of spring ArrayList getmapping response

I have a Spring Boot application that has a getmapping returning an ArrayList. This all works fine and it returns the arraylist as a json object in the body. I am trying to write a postman test on the response to verify the length of the arraylist that comes back. I cant seem to get it to find the arraylist on the object however since there doesn't appear to be any keys.
My Spring Code is:
#GetMapping(value="/getAllCars", produces = "application/json")
public #ResponseBody List<Car> getAllCars() {
return carManager.getAllCars();
}
The response in postman shows
[
{
"id": "19a94f8d-b1c1-4814-abba-57b1cb319478",
"name": "car1"
},
{
"id": "4e2568ca-f41a-480d-82b5-acede9109a6d",
"name": "car2",
},
{
"id": "4e2568ca-f41a-480d-82b5-acede9109a6d",
"name": "car3",
},
... (there is 72 total)
]
my test code in postman is is
pm.test("CarsLength" , function(){
const responseJson = pm.response.json;
pm.expect(responseJson.length).to.equal(72);
})
when i look at the postman console after logging the responseJson and responseJson.length its only logging out the function name and 2. responseJson[0] and responseJson[1] are both undefined when i log them out as well.
I dont have a key in my response json object is thats whats causing this? How can I access the objects to verify the length in a postman test?
The mistake is here:
pm.response.json --> pm.response.json()

Quarkus calling Rest API with dynamic endpoint

I used the micro profile to make a call to a specific API, it happens that this call gives me a list of URLs and I have to make a call for each URL returned.
My objective is call all of these URL e verify HTTP status returned.
"ApiResources": [
{
"ApiFamilyType": "products-services",
"ApiVersion": 1,
"ApiResourceId": "d33ff8af-5a1b-4b43-90d4-34237997d080",
"ApiDiscoveryEndpoints": [
{
"ApiEndpoint": "https://api.yyyyyyyy/v1/personal-accounts"
},
{
"ApiEndpoint": "https://api.xxxxxxx/business-accounts"
}
}
]

Micronaut GraphQL: How to respond with a non-200 HTTP status code from within GraphQL handler?

Following the docs and here's my exception handler (Kotlin):
#Produces
#Singleton
#Requirements(Requires(classes = [ForbiddenException::class, ExceptionHandler::class]))
class ForbiddenExceptionHandler : ExceptionHandler<ForbiddenException, HttpResponse<*>> {
override fun handle(request: HttpRequest<*>, exception: ForbiddenException): HttpResponse<*> {
return HttpResponse.status<String>(HttpStatus.FORBIDDEN, exception?.message)
}
}
Throwing a ForbiddenException from within my GraphQL handler bubbles the message into the response body, but the status code is always 200.
Example response:
{
"errors": [
{
"message": "Exception while fetching data (/createUser) : FORBIDDEN",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"createUser"
],
"extensions": {
"classification": "DataFetchingException"
}
}
],
"data": null
}
Micronaut version: 1.3.3
Micronaut GraphQL version: 1.3.0.RC1
Disclaimer:
GraphQL is not REST. You are here asking a question related to the core foundation of graphql specification (and any implementations of graphql in general).
They made the choice to embed most errors encountered in the execution of the queries but yet always return a 200 HTTP status. Therefore, you won't be able to change that in your project. It is not a configuration of graphql-java.
The good news is that the format of errors is known. Therefore, you are able to deserialize the error return payload in your application and handle correctly any error that would be thrown by graphql.
Please have a look at this link for in-depth explanations about the main difference between REST and Graphql.

MailChimp API 3.0 batch request returns 400 Invalid Resource error

I've been trying to use the batch endpoint of MailChimp API (version 3.0) to subscribe new users to a list, but can't make it work.
Here is the request:
POST /3.0/batches
{
"operations": [
{
"method" : "POST",
"path" : "lists/c852ce5c86/members",
"body": "{\"email_address\":\"email#domain.tld\", \"status\":\"subscribed\"}"
}
]
}
The request seems ok cause I get a 200 response:
{
"id": "49abca6ef3",
"status": "finished",
"total_operations": 1,
"finished_operations": 1,
"errored_operations": 1,
"submitted_at": "2015-09-21T18:11:16+00:00",
"completed_at": "2015-09-21T18:11:23+00:00",
"response_body_url": "https://mailchimp-api-batch.s3.amazonaws.com/49abca6ef3-response.tar.gz?..."
}
However, as you can see, the only operation in my batch is errored.
Here is the response_body_url for this operation:
[{
"status_code":400,
"operation_id":null,
"response":"{
\"type\":\"http://kb.mailchimp.com/api/error-docs/400-invalid-resource\",
\"title\":\"Invalid Resource\",
\"status\":400,
\"detail\":\"The resource submitted could not be validated. For field-specific details, see the 'errors' array.\",
\"instance\":\"\",
\"errors\":[{
\"field\":\"\",
\"message\":\"Schema describes object, NULL found instead\"
}]
}"
}]
which is not very helpful :(
Note that if I directly hit POST lists/c852ce5c86/members with {"email_address":"email#domain.tld", "status":"subscribed"} payload, it's working properly.
That was actually a bug in the mailchimp API. After reaching them they quickly fixed it.

Resources