Question
Raghu Kodumuri · Aug 17, 2016

Setting ContentType in Rest Service

Hello,

I am working on a Rest Service that should accept a particular content type. How and where can I set the value?

I have tried setting the value %response.ContentType by overriding Page method OnPreHttp()  but from what I see this method never got executed. 

 

Thanks

Raghu

0
0 1,696
Discussion (13)1
Log in or sign up to continue

If you use %CSP.REST, you can set needed ContentType in any called method

XData UrlMap
{
<Routes>
  <Route Url="/text" Method="GET" Call="GetText" Cors="false" />
</Routes>
}

ClassMethod GetText() 
{
    set %response.ContentType="text/plain"
    
    write "test"
    
    quit $$$OK
}

and test

➜  ~ curl -v http://localhost:57774/api/app/text
*   Trying ::1...
* Connected to localhost (::1) port 57774 (#0)
> GET /api/ambulance/text HTTP/1.1
> Host: localhost:57774
> User-Agent: curl/7.49.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Wed, 17 Aug 2016 17:54:28 GMT
< Server: Apache
< CACHE-CONTROL: no-cache
< EXPIRES: Thu, 29 Oct 1998 17:04:19 GMT
< PRAGMA: no-cache
< CONTENT-LENGTH: 4
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact
test%

Thank you for trying to help me. I am not looking to set the Response ContentType. I am trying to set accepted content type.

 

Thanks

Raghu

Well, in this case you should set Header Accept, with needed one or more formats, more info in wiki

set %response.SetHeader("Accept","text/plain")

Where do you suggest me to add that line? In any method or OnPreHttp() method?

Dmitry,

 

That did not help. Let me phrase my question different way, by Default ContentType of a Rest business service is text/html, I want that to change to  application/x-www-form-urlencoded. When my client posts the data as application/xml or text/xml. I am able to extract the stream from Input object. But when my client sends the contant type as application/x-www-form-urlencoded, in service class I am getting empty stream.

 

So I am trying to match the content type with the client.

 

Thanks

Raghu

So, you had to start with such explanation.

Well, does not matter what do you set in Accept header, if you don't use it by yourself. Like, you should check incoming content type and send an error if it is not accepted. This Header change nothing in incoming data,  if data was sent in another format.

To read data, you should know that %request has three different ways for getting data. You have already known in %request.Content, which usually contains binary streams. Then %request.MimeData, and %request.Data, it is a Multidimensional properties, and %request has some getters for them, %request.GetMimeData and %request.Get. MimeData, needs when client send data in multipart mime format, such as several files or so on. And %request.Data, in all most cases, and you should look at this property and method %request.Get("somename")

Thank you, Dmitry. Your direction helped me. I found the document that explains the %request object.  So, in this case pInput parameter doesn't hold any info?

 

Thanks

Raghu

As I see you a talking about Ensemble REST service. In this case I am not 100% sure, I think pInput only contains some binary data, or json. But forms data any should be available with %request.

If you are publishing a RESTful service then you don't set the Accepted Content type.  This is for the client to inform you as to what they responses they can accept.  So your rest service would examine this value to verify that you can supply the type of response the client will accept.  You would access this value using this syntax:

 

%request.CgiEnvs("HTTP_ACCEPT")

Hi Raghu,

Stream objects have an Attributes collection which holds the incoming HTTP headers.  I believe you should be able to check the incoming Content Type with pInput.GetAttribute("Content-Type") but I haven't confirmed this.

-Brendan

Thank you, Brendan. I am already doing this to find out what is the content type of incoming request.

 

Thanks

Raghu