Question
· 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

Discussion (13)0
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%

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")

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")