Azure API Management - Defining Custom Error - azure-api-management

I'm using Azure API management for my project, with a backend service provided by Fiorano ESB. Fiorano to Azure has a peer-to-peer connectivity.
The services is deployed in the peer server which is added as a cloud service in azure.
So my concern is : for an API, in the URL if the resource is missing or wrong by default we'll get a json response as
{
"stausCode" : 404,
"message" : "Resource not found"
}
Is there any way that I can define this in OData standard format i.e. can i define it in a custom format as given below:
{
"error":{
"code" : "404_RES",
"message":{
"lang":"en-uk",
"value":"Resource $(wrong_resource given) is not found"
},
"innererror":{
"trace":[],
"context":{}
}
}
}
The $(wrong resource) should be taken dynamically

Take a look at set-body policy: https://msdn.microsoft.com/en-us/library/azure/dn894083.aspx#SetBody.
Basically, it allows you to modify/replace message bodies of incoming and outgoing requests.

Related

How to send Whatsapp message using MessageBird from Firebase cloud function?

I have installed MessageBird Firebase Extension.
I want to send Whatsapp template(template has Button with website link) message from Firebase Cloud Functions.
To send a Whatsapp message, I need to create a document in Firebase 'messages' collection, so that MessageBird extension processes it.
What should be the format of Firebase document to send Whatsapp message? I have attached the Whatsapp template
I have tried creating Firebase document similar to this API request format - https://developers.messagebird.com/quickstarts/whatsapp/send-message-with-buttons/, but I got delivery error 'Error: api error(s): JSON is not a valid format (code: 21)'.
Thank you.
You are right, you need to put a document with content into Firestore message collection that is accepted by ConversationsAPI, and then the message should be sent and the document should be updated with delivery details (response from MessageBird Conversations API).
This is example of the document that you will need to put to be able to send WA template message:
db.collection('YOUR_DOCUMENT_COLLECTION').add({
channelId: 'YOUR_CHANNEL_ID',
type: 'hsm',
content: {
hsm: {
namespace: 'YOUR_WA_ACCOUNT_NAMESPACE_ID',
templateName: 'YOUR_WA_TEMPLATE_NAME',
params: [{ default: 'YOU_PARAM_VALUE' }],
language: { code: 'en_US', policy: 'deterministic' }
}
},
to: 'RECIPIENT_OF_THE_MESSAGE',
});
You can find more details about HSM object and what each field means here: https://developers.messagebird.com/api/conversations/#messagehsm-object
I hope that helps.

Rest APIS - HTTP Delete - Request body gets ignored/dropped WSO2 API Manager

Environment :-
Web Service - Rest APIs.
Media Type - JSON.
JAX RS implementation - Jersey.
HTTP Methods - POST, GET, PUT & DELETE.
WSO2 API Gateway Manager - To sit on top of it to publish these APIs.
Problem :-
The delete operation has a request body(a json message) which has input values in it. When I hit the services directly - the whole operation performs successfully.
But once I go through the API Manager (using the URL that it publishes) - the request body seems to be getting dropped/ignored & hence the operation fails.
Rest all the operations are working fine & so is the OAuth too.
Can someone please help me - as to why is the the request body (a json message) is getting discarded when I go through the WSO2 API gateway manager - and what can I do to get it working, please !!
Thanks in advance.
HTTP DELETE opration isn't supposed have body. You can check here and here
Many frameworks discourage using it or warn you that the body may be dropped. The reason is that you want to DELETE some resource identified by your URI thus no body should be required.
Update
WSO2 API Manager depends on Apache HttpComponents/HttpCore library for this functionality and this issue is fixed in their 5.0 which isn't released yet.
You can find the JIRA here reported against httpCode library.

Why my wso2 esb api return only the first property of the original json?

I'm using wso2 esb 4.0.3.
I used cxf to create a jaxrs api. The first resource is simple. I use SOAP-UI to issue an HTTP GET to this resource, and it returns a json string with the data I expected. Like:
{"id":"some id", "name": "some name", "when":"some time".......}
I created an API in wso2 esb. A resource is configured to call the backend resource mentioned above. When I call this resource using SOAP-UI, I get a json string, but with only the first property of the data. Like:
{"id":"some id"}
What is causing this problem, and how can I fix this?

json conversion issue in wso2 API Manager

We are trying to use wso2 api manager to access some of our RESTful services.
The rest services expect a json payload. When json request is sent to api manager it converts the json request to xml and again while forwarding to the actual endpoint it converts to json back.
The resulting json is not matching exactly the original request in one of our cases.
For instance if the rquest contains an array of elements and if only one element is passed in the array then when api manager forwards the request to the endpoint the array characters ([,]) are removed.
eg.
our original request was
{
"entities": [
{
"name":"KK71CP20000523A1",
"descr":"VaS",
"mnf":"BCT",
"mdlyr":"2012"
}
]
}
the request sent by api manager was
{
"entities":
{
"name":"KK71CP20000523A1",
"descr":"VaS",
"mnf":"BCT",
"mdlyr":"2012"
}
}
The array wrapping is removed under entities element.
When the number of elements is more than one then the array characters are retained.
We faced the same issue in ESB as well previously. But we worked around the issue by extending the default JSONMessageFormatter and using the seriliazeAsArray method available in the jettison library.
But we dont want to do this customization in API Manager.
Is there a better way of fixing this issue? Any patch available from wso2 to fix this?
All carbon products comes with same message builders and formatters, for your case can you check with JSONStreambuilder and formatter.
Look at this reference,

Should HTTP Status be used in Restful Error Responses?

I'm writing a Restful API and I have to return error message but I'm not sure on which route to go.
Route 1 - HTTP Status
Use HTTP error status when the client sends bad data
Ex: 401 - Not authorized, 410 - Model does not exist, 412 - Model Validaiton Error, etc
Route 2 - JSON Success or Error Failure
The API returns json and I am considering returning everything with the http header 200, but then in my JSON handle errors and success
Ex:
{ "status" : "error", "message" : "Model validation error", "data" : ["user name required", "user email required"] }
Which route should I go and why? Advantages and disadvantages.
I'm writing a Restful API and I have to return error message but I'm
not sure on which route to go.
Route 1 - HTTP Status
Use HTTP error status when the client sends bad data
HTTP status codes should absolutely be used in any web service implementation claiming to be RESTful. The core principle of the specification is leveraging and extending the Web to fully support transfer of representational state. To allow for interoperation with existing Web infrastructure a REST implementation should indicate status of requests via appropriate HTTP status codes. For example:
200 - Ok
201 - Content Created
401 - Unauthorized
403 - Forbidden
500 - Server Error
501 - Not Implemented
When responding with many of these statuses, its also allowed by the HTTP specification to include an entity representation in the response body. In the case of 'normal', non-error responses, this representation will normally be of the entity that is being 'operated' on by the HTTP request. In the case of error responses the representation, if included, should provide more information on the error that occurred. This is where we segue to your option 2.
Route 2 - JSON Success or Error Failure
The API returns json and I am considering returning everything with
the http header 200, but then in my JSON handle errors and success
You should absolutely not return a 200 OK for all responses. Many well implemented HTTP clients depend on the status code in the response to determine wether it succeeded or not. Always responding with a 200 OK can cause third party client libraries to incorrectly process the incoming data, and also puts a requirement on your client to parse the response body in order to determine if an error actually happened or not.
Having said that, adding additional information about the error that occurred can be very helpful, so definitely do consider adding it to the response body. Your proposed format looks just fine, though to be honest the status element is redundant, assuming you use HTTP status codes appropriately. Something more like:
{
"message": "Model validation error",
"data": [
"user name required",
"user email required"
]
}