Question
James Westley-F... · Dec 7, 2021

Can't change content-type in REST response

I've never had a problem changing the content-type on a response using either %CSP.Page or %CSP.REST until now. No matter where I put the line to set the content-type to "application/json", it stubbornly emits a content-type of "text/html".

In OnPreHTTP: (I tried one at a time)
either
    do %response.SetHeader("content-type", "application/json") // didn't work
or

    set %response.ContentType = "text/plain" // didn't work

The same lines in OnProcessInput() didn't change the output content-type, either.

The class signature is 
Class HS.Local.VA.HS.DocRepo.Prefetch.Service.RESTService Extends (Ens.BusinessService, %CSP.REST)

I get the data I'm looking for back in JSON format the way I expected. My consuming app doesn't like the fact that content-type is always text/html.

I've written tons of CSP and never had a problem changing content-type. Not sure what's going on here.

Thanks in advance!!

Bill

Product version: HealthShare 2019.1
$ZV: Cache for Windows (x86-64) 2018.1.2 (Build 309_5_19491U) Tue Dec 10 2019 17:30:27 EST
0
0 221
Discussion (4)2
Log in or sign up to continue

FWIW, I tried curl as well with the same results

C:\Users\VHADURWestlJ>curl --verbose  -H "Accept: application/json" --basic --http1.1 --verbose --user myusername:mypassword http://localhost:57772/csp/prefetch-rest/smoketest
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 57772 (#0)
* Server auth using Basic with user 'bfarrell'
> GET /csp/prefetch-rest/smoketest HTTP/1.1
> Host: localhost:57772
> Authorization: Basic YmZhcnJlbGw6ejAwMTA2MTUr
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 07 Dec 2021 19:54:43 GMT
< Server: Apache
< TRANSFER-ENCODING: chunked
< EXPIRES: Thu, 29 Oct 1998 17:04:19 GMT
< CACHE-CONTROL: no-cache
< PRAGMA: no-cache
< SET-COOKIE: CSPSESSIONID=000100010000MIqK2mHTrn000000000000000000000000000000000001; path=/;
< Content-Type: text/html; charset=utf-8
<
{
"errors": [
],
"exception": "",
"messages": [
],
"recordset": [
],
"sessionId": "MIqK2mHTrn",
"smokeTest": "this is a smoke test with some fake JSON return",
"summary": "success"
}* Connection #0 to host localhost left intact

I don't know if this is the case, but be sure not to output anything before setting Content-Type or HTTP status.
In my case it was a debug Write statement that wrote directly to the current TCP stream.
Because of IO redirect when something is written to the stream (the HTTP body) first the headers must be written and those are at that time the default values.

Hi Herman,

Thanks for the reply. That was the first thing I thought of and looked for. I'm using %OnPreHTTP() so nothing can be output before that finishes.

There's certainly something else going on here.

Bill

Hi Bill, I was facing a similar (but not identical) issue not too long ago where I was trying set %response.Status in my REST handler class, only to have a different status code reflected in the response to the client.

The issue ended up being that the request to the REST handler class that I was working on was being forwarded from another REST handler class that extends EnsLib.REST.Service rather than %CSP.REST.  The EnsLib REST handler works a little differently.  The user code is supposed to write the output to a response stream, with the response headers being set as attributes of the stream, rather than setting headers of %response directly.

So my question is, is your REST handler downstream from an EnsLib.REST.Service REST handler?  I also see that your REST handler extends Ens.BusinessService.  I wonder if something in that class is overriding your %response headers.  Is there any way you can test your class with that superclass removed?