Send custom error response in Spring Cloud Gateway when no route matches - spring

In Spring Cloud Gateway , I want to send a custom error in case a particular predicate match fails . For eg:-
I have a Path and a header predicate. Something like this -
- id: test
uri: http://localhost:8000/
predicates:
- Path=/consignment
- Header=h,h1
So if the path mathces and header doesn't , the request fails with not found error something like by default-
{
"timestamp": "2020-03-08T20:05:42.440+0000",
"path": "/test",
"status": 404,
"error": "Not Found",
"message": null,
"requestId": "6dd2e799"
}
But I want to send a custom response mentioning that the header is not present.
Is there a way to do that?

Related

Unable to get a security group ID by name using Graph API. Error "URI path is not a valid Graph endpoint," inside our Power Automate flow

I want to get a security group ID by name, so i tried this HTTP request action, by passing this uri (https://graph.microsoft.com/v1.0/groups?$filter=displayname eq 'SEC-Project Repository Hub-Visitors'):-
but i got this error
{
"error": {
"code": 400,
"message": "URI path is not a valid Graph endpoint, path is neither absolute nor relative or resource/object is not supported for this connector. Resources: groups. Uri: https://graph.microsoft.com/v1.0/groups?$filter=displayname eq 'SEC-Project Repository Hub-Visitors'",
"source": "canada-001.azure-apim.net",
"path": "choose[10]\\when[1]",
"policyId": "",
"clientRequestId": "*****"
}
}
any advice please?
I saw some posts about it. Just put / after groups:
groups/?....

How to send error response as json in Grpc using envoy-proxy

I am throwing error from grpc service using resonseObserver.onError() but I am not getting messages in json format while hitting REST API from rest client, though the positive scenario is working fine and giving response as json.
I am using envoy as a transcoder, can anyone help me with how to get error response also as json. Currently I am getting BadRequest on error scenarios. The project is in SpringBoot.
TIA
You can use convert_grpc_status: true for do this.
http_filters:
- name: envoy.filters.http.grpc_json_transcoder
typed_config:
"#type": type.googleapis.com/envoy.extensions.filters.http.grpc_json_transcoder.v3.GrpcJsonTranscoder
proto_descriptor: "/tmp/envoy/proto.pb"
services: ["xxxxxxxx"]
convert_grpc_status: true
print_options:
always_print_primitive_fields: true
always_print_enums_as_ints: false
preserve_proto_field_names: false
If you mean return details key like this:
{
"code": 3,
"message": "API call quota depleted",
"details": [
{
"#type": "type.googleapis.com/google.rpc.ResourceInfo",
"resourceType": "xxxxxx",
"resourceName": "",
"owner": "",
"description": ""
}
]
}
You MUST compile you .proto file with:
import "google/rpc/error_details.proto";
because Envoy can't deserialize binary details from your backend server without error types.
Also you can read how you can send detailed error response with Python: How to send error details like as BadRequest

Google Speech API: the requested URL was not found on this server

I am attempting some simple tests on the Google Speech API, and when my server makes a request to this url (below), I get the 404. that's an error response. Not sure why.
https://speech.googleapis.com/v1/speech:recognize?key=[MY_API_KEY]
The body of my request looks like this:
{
"config": {
"languageCode": "en-US",
"encoding": "LINEAR16",
"sampleRateHertz": 16000,
"enableWordTimeOffsets": true,
"speechContexts": [{
"phrases": ["Some", "Helpful", "Phrases"]
}]
},
"audio":{
"uri":"gs://mydomain.com/my_file.mp3"
}
}
And here is the response:
As you can see, that is a valid resource path, unless I'm totally mistaken about something (I'm sure I am): https://cloud.google.com/speech-to-text/docs/reference/rest/v1/speech/recognize
Update 1:, Whenever I try this with the Google API explorer tool, I get this quota exceeded message (even though I have not yet issued a successful request to the API).
{
"error": {
"code": 429,
"message": "Quota exceeded for quota metric 'speech.googleapis.com/default_requests' and limit 'DefaultRequestsPerMinutePerProject' of service 'speech.googleapis.com' for consumer '[MY_API_KEY]'.",
"status": "RESOURCE_EXHAUSTED",
"details": [
{
"#type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developer console API key",
"url": "https://console.developers.google.com/project/[my_project_id]/apiui/credential"
}
]
}
]
}
}
Update 2: Interestingly, I was able to get some 200 ok's using the Restlet client, but even in those cases, the response body is empty (see screenshot below)
I have made a test by using the exact URL and Body content you added to the post, however, I was able to execute the API call correctly.
I noticed that if I add some extra character to the URL, it fails with the same 400 error since it doesn't exist. I would suggest you to verify that the URL of your request doesn't contain a typo and that the client you use is executing the API call correctly. Also, ensure that your calling code is not encoding the url, which could cause issues given the colon : that appears in the url.
I recommend you to perform this test by using the Try this API tool directly or Restlet client which are the ones that I used to replicate this scenario.

Getting a 404 to this API

I have an API here: https://github.com/BillyCharter87/Tech-O-Dex-API/tree/AddingSpringJPA
I continue to get a 404 on either the /greeting or /health. The application starts up fine and deploys on 8080, I've also tried changing the listener port to something else but to no avail.
My Request:
{
"firstName": "Billy",
"lastName": "Charter"
}
Headers:
Content-Type: application/json
Accept: application/json
Response I get back:
{
"timestamp": "2018-04-22T16:20:30.874+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/greeting"
}
Thanks!
SpringBoot main class scans the folder "below" to find components.
Your Application.java is at the same level as your components, so none of your spring components are found.
Reorganize your project's sources as follow:
com/projectname/Application.java
com/projectname/controller/GreetingController.java
com/projectname/service/GreetingService.java
com/projectname/model/...
com/projectname/dto/...
com/projectname/dao/...
Also, while you're at it, remove the #Component on your DTO.

403 Forbidden resposnse from a GET request to http://api.espn.com/v1/sports/news/6277112

I have problem with the ESPN API:
I have an API key, and sent a query to the address http://api.espn.com/v1/sports/news/1581816?region=GB, which included my API key in the parameters.
I received 403 forbidden response: with this JSON
{
"status": "error",
"code": 403,
"message" : "Account Inactive"
}
I am looking at http://developer.espn.com/docs/headlines#using-the-api and at the last line it says that i can make a GET request to this url and it should work, but it doesn't.
How can I receive a valid response from the ESPN servers?
I just test it and it works fine. That means your key is invalid, or you are not including it. Replace below and it will work.
http://api.espn.com/v1/sports/news/1581816?region=GB&apikey=<yourKey>
--
{
timestamp: "2013-10-13T15:33:49Z",
resultsOffset: 0,
status: "success",
resultsLimit: 10,
resultsCount: 1,
headlines: [
{ ... }
]
}

Resources