ApiMethodException content-type result

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

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

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

Hi,

You can use server.HttpRequest event.

it works similar to HTTPServer.OnCustomResponseEvent (Delphi), i.e. with unknown requests.