Hello,
I need to return a json structure instead of a plain text raising ApiMethodException
(using message parameter o something else extending the exception).
Is it possible to change this specific response content-type to ApiResultContentType.Json
?
Thank you,
Giovanni
EvgenyK
(Evgeny Karpov)
September 17, 2024, 8:29am
2
Hi,
I can recommend to use ApiResult class.
see more at HttpAPI in details
Thank you Evgeny,
but I prefer to use ComplexType
descendants as results of services methods instead of Binary
as requested using ApiResult
Would it be handling the exception to change content-type message on occurrence?
Thank you,
Giovanni
EvgenyK
(Evgeny Karpov)
September 17, 2024, 12:00pm
4
Hi,
ApiMethodException exception is written as a plain text so you can’t use it for returning of a json structure …
private void HandleServiceMethodException(IHttpResponse response, Stream responseData, Exception ex)
{
...
// Custom ApiMethod exceptions
ApiMethodException customException = ex as ApiMethodException;
if (customException != null)
{
this.WriteErrorResponse(response, responseData, customException.ErrorCode, customException.Message);
return;
}
private void WriteErrorResponse(IHttpResponse response, Stream responseData, HttpStatusCode errorCode, String errorMessage)
{
response.HttpCode = errorCode;
if (!String.IsNullOrEmpty(errorMessage))
{
response.ContentType = ApiResultContentType.Text;
StreamHelpers.Utf8StringToStream(errorMessage, responseData);
}
}
I see, thank you.
Is there a chance to manipulate the return message just in case
(as you already suggest in Change HTTP status code as needed - Remoting SDK - RemObjects Talk for Delphi)?
Thank you,
Giovanni
EvgenyK
(Evgeny Karpov)
September 17, 2024, 2:31pm
6
Hi,
You can use server.HttpRequest event.
it works similar to HTTPServer.OnCustomResponseEvent (Delphi), i.e. with unknown requests.