Sidebar

How do I set the 'Content-Type' in an HTTP Response?

0 votes
837 views
asked Aug 13, 2014 by mike-r-7535 (13,830 points)
I am using JSON, and need to set the 'Content-Type' to 'application/json'.  How do I do this?

3 Answers

0 votes

In order to set the content-type of the http response, you will need to prepend your message content with actual http header for Content-Type.  So, you will want to set the content type something like this:

var contentType = "application/json";

var contentTypeLine = "http.header.Content-Type=" + contentType + "\r\n\r\n";

qie.postMessageResponse(contentTypeLine + message.getNode('/'));

Note: The prefix "http.header." before the header name (in this case Content-Type) followed by "=" and the header value, and a carriage return line feed (\r\n) are required.

 

See also:

Handling HTTP requests and responses

Posting Responses via qie.postMessageResponse() in a channel that uses an HTTP Listener source node

answered Aug 13, 2014 by mike-r-7535 (13,830 points)
edited May 17 by rich-c-2789
0 votes

Another example to add an http header to a message response is the following

qie.postMessageResponse('http.header.Access-Control-Allow-Origin=*\r\n' + message);

answered May 8, 2017 by brandon-w-8204 (33,370 points)
edited May 2 by rich-c-2789
0 votes

Here is an example of returning multiple headers.

They all should start with "http.header." and end with "\r\n"

Example:

qie.postMessageResponse('http.header.Access-Control-Allow-Origin= *\r\nhttp.header.Transfer-Encoding=chunked\r\n' + message);

answered Dec 5, 2022 by brandon-w-8204 (33,370 points)
edited May 2 by rich-c-2789
...