Returning alternative headers

In the new HTTP API, how can I return a response data type, to indicate JSON or other MIME type?

TROHttpApiDispatcher can return data only in application/json; charset=utf-8 format.

You can create descendant of TROHttpApiDispatcher and override Process method:

procedure Process(
   aTransport: IROHTTPTransport; aRequest: IROHTTPRequest; 
   aResponse: IROHTTPResponse; aRequestData: TStream; aResponseData: TStream); override;

in this case, you can modify headers and response data if it is needed.

another possibility with similar functionality - to use HTTPServer.OnCustomResponseEvent as described in Using OnCustomResponseEvent in a ROSDK Server article.

Thank you. The custom Process may be an option, but it isn’t really designed to work as a custom intercept. I’ve always had to customise the code instead of using the TROJavaScriptHttpDispatcher because the OnRequest doesn’t have the option to revert to normal processing if you don’t want to customise it. The OnCustomResponseEvent is not firing on my system - not sure why but I have a breakpoint there and it never gets hit.

Anyway, it’s a current limitation, not a show-stopper. Hopefully a means to get to this sort of detail will be included at some point.

it can handle only unknown requests:

 // handle all requests started with 'xxxx':
  if pos('/xxxx',aTransport.PathInfo) = 1 then begin  // "xxxx" shouldn't be defined in Server.Dispathers

Hello

We have some plans on supporting custom-type responses, yet no concrete ETA for them is available atm

In TROHTTPFileDispatcher.Process there is a call to ServeCustom but it doesn’t have any way to go through to the event. The event itself is never called, even if I do a POST to an path that doesn’t exist. If you can tell me what to send to make it trigger, that would be good. Thank you.

you should use OnCustomResponseEvent event of http server instead of events of dispatchers.

if you have http api is set to api , try to call xxxapi.
Note: this path shouldn’t be handled by dispatchers.

you will be able to catch request to xxxapi in this event.
once it was caught, you can modify and repass it to your http api component, handle it and modify response headers and stream as you want.

I shall get my demo project to you, and you can see it so you can tell me what I’m doing wrong to not get this event called.