# Using OnCustomResponseEvent in a ROSDK Server

**URL:** https://talk.remobjects.com/t/using-oncustomresponseevent-in-a-rosdk-server/6767
**Category:** Snippets
**Tags:** httpserver, rosdk, delphi
**Created:** [August 28, 2015, 1:15pm UTC](https://talk.remobjects.com/t/using-oncustomresponseevent-in-a-rosdk-server/6767 "2015-08-28T13:15:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [August 28, 2015, 1:15pm UTC](https://talk.remobjects.com/t/using-oncustomresponseevent-in-a-rosdk-server/6767/1 "2015-08-28T13:15:27Z")

</div>

example for plain [HTTPServer.OnCustomResponseEvent](https://docs.remotingsdk.com/API/Delphi/Classes/TROBaseHTTPServer/#OnCustomResponseEvent).  
This snippet shows how to handle unknown requests or requests with incomplete data.  
[TROPostMessage](https://docs.remotingsdk.com/API/Delphi/Classes//TROPostMessage/) was used

```auto
procedure TServerMainForm.ROServerCustomResponseEvent(
  const aTransport: IROHTTPTransport; const aRequestStream,
  aResponseStream: TStream; const aResponse: IROHTTPResponse;
  var aHandled: Boolean);
var
  lMessage: IROMessage;
  op: TROResponseOptions;
  s: AnsiString;
begin
  // handle all requests started with 'xxxx':
  if pos('/xxxx',aTransport.PathInfo) = 1 then begin // "xxxx" shouldn't be defined in Server.Dispathers
    
   //1. Pre-processing data 
{ 
   // * extracting data if GET method was used
   s := aTransport.QueryString;
    
  // * extracting data if POST method was used
    SetLength(s, aRequestStream.Size);
    aResponseStream.Position := 0;
    aRequestStream.Read(Pointer(s)^, Length(s));

   // modify data in some way
    s := ' __MessageType=Message&__ InterfaceName=MyService&'+s;

   // write updated data back to stream
    aRequestStream.Size := 0;
    aRequestStream.Write(Pointer(s)^, Length(s));
}
    // 2. Invoke service method 
    lMessage:= (ROPOSTMessage as IROMessageCloneable).Clone;
    MainProcessMessage(lMessage,atransport,aRequestStream,aResponseStream,op);

    //3. Post-processing data
{ SetLength(s, aResponseStream.Size);
    aResponseStream.Read(Pointer(s)^, Length(s));

    // modify data again
    s := s + my_some_data;

    aResponseStream.Size := 0;
    aResponseStream.Write(Pointer(s)^, Length(s));
}
    aHandled := True;
  end;
end;

```

---

<div class="post-metadata">

### Author: ![triplem](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/t/b19c9b/32.png) [@triplem](https://talk.remobjects.com/u/triplem)
#### Post date: [August 2, 2022, 1:08pm UTC](https://talk.remobjects.com/t/using-oncustomresponseevent-in-a-rosdk-server/6767/2 "2022-08-02T13:08:59Z")

</div>

One question about this snippet above: If i get this right i have to use only a TROHTTPServer without TROHttpApiDispatcher and without any TROMessage?

If i try this an debug this event using my browser with url `http://localhost:8443/lpr/` everything ist empty except PathInfo and ClientAddress. How could one get the other infos like is it a get/post request and so on?

 ![debug](https://talk.remobjects.com/uploads/default/original/3X/7/0/70d1428ff63a471579076b33465c92a03e20e219.jpeg)

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [August 2, 2022, 1:18pm UTC](https://talk.remobjects.com/t/using-oncustomresponseevent-in-a-rosdk-server/6767/3 "2022-08-02T13:18:05Z")

</div>

Hi,

> [@triplem](#):
>
> One question about this snippet above: If i get this right i have to use only a TROHTTPServer without TROHttpApiDispatcher and without any TROMessage?

not yet. However this event is fired if request wasn’t processed by Messages or HttpApiDispatcher.

> [@triplem](#):
>
> If i try this an debug this event using my browser with url `http://localhost:8443/lpr/` everything ist empty except PathInfo and ClientAddress. How could one get the other infos like is it a get/post request and so on?

this is correct. your browser doesn’t fill another info. try to use CURL.  
in CURL request you can provide another info  
for example:

```auto
curl -X POST "http://localhost:8099/xxxx/readme" -H "accept: application/json" -H "content-type: application/json" -d "{ \"Address1\": \"123 Main St\", \"Address2\": \"STE B\", \"City\": \"New York\", \"State\": \"NY\", \"ZipCode\": \"10025\" }"

```

---

<div class="post-metadata">

### Author: ![triplem](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/t/b19c9b/32.png) [@triplem](https://talk.remobjects.com/u/triplem)
#### Post date: [August 2, 2022, 2:07pm UTC](https://talk.remobjects.com/t/using-oncustomresponseevent-in-a-rosdk-server/6767/4 "2022-08-02T14:07:18Z")

</div>

Hi Evgeny,  
could you please update your 7 years old code above. I can’t even compile it because of unknown ROPOSTMessage. Furthermore i don’t really understand what to do. I have mainly rest GET requests where i have to respond as XML. And also some POST pequests with i have to answer also with an XML Response.

I don’t understand why and in what case i have to modify the RequestStream? could you create a small example with a GET and a POST request please? I would highly appreciate it.  
Thanks  
Werner

Delphi 11.1 / RO 10.0.0.1537

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [August 2, 2022, 2:22pm UTC](https://talk.remobjects.com/t/using-oncustomresponseevent-in-a-rosdk-server/6767/5 "2022-08-02T14:22:10Z")

</div>

> [@triplem](#):
>
> I can’t even compile it because of unknown ROPOSTMessage.

this is component on form

```auto
ROPOSTMessage: TROPOSTMessage;

```

because this sample uses TROPostMessage.

check related threads. they contain examples for HttpApi and another types of messages.

* * *

> [@triplem](#):
>
> I don’t understand why and in what case i have to modify the RequestStream?

some users want to update RequestStream and add extra data/parameter.  
you can ignore `Pre-processing data` section if you don’t it.

* * *

> [@triplem](#):
>
> could you create a small example with a GET and a POST request please?

above example already contains it.

```auto
   if (aTransport as IROHTTPRequest).GetMethod = 'GET' then begin
     // s: AnsiString;
     s := aTransport.QueryString;
     aRequestStream.Size := 0;
     aRequestStream.Write(Pointer(s)^, Length(s));
   end;
   if (aTransport as IROHTTPRequest).GetMethod = 'POST' then begin
     // no changed is needed, request stream is already in aRequestStream
   end;

```

---

<div class="post-metadata">

### Author: ![triplem](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/t/b19c9b/32.png) [@triplem](https://talk.remobjects.com/u/triplem)
#### Post date: [August 2, 2022, 2:53pm UTC](https://talk.remobjects.com/t/using-oncustomresponseevent-in-a-rosdk-server/6767/6 "2022-08-02T14:53:12Z")

</div>

Thanks Evgeny, last question. Where can i set the http request result error state? I have set it to 500 in any error case.

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [August 3, 2022, 6:53am UTC](https://talk.remobjects.com/t/using-oncustomresponseevent-in-a-rosdk-server/6767/7 "2022-08-03T06:53:00Z")

</div>

Hi,

use the [IROHTTPResponse.Code](https://docs.remotingsdk.com/API/Delphi/Interfaces/IROHTTPResponse/#Code) property:

```auto
aResponse.Code := 500;

```
