imrefazekas / connect-rest

Exceptionally featureful Restful web services middleware for connect node.js
MIT License
100 stars 29 forks source link

Is there a way to return content as is, rather than converting it to JSON? #10

Closed behrangsa closed 10 years ago

behrangsa commented 10 years ago

In particular I like to render response as XML, but right now it is getting wrapped inside double quotes.

behrangsa commented 10 years ago

I had a look at the source code and it turns out this will work:

restSupport.get({ path: '/somePath''}, function (req, content, next) {
      var response = "<xml>some xml</xml>";
      next(null, response, {});
   }, {contentType: 'application/xml'});
imrefazekas commented 10 years ago

Well, if you specify the response mime-type you won't send back quoted string.

In code: res.end( (result.contentType == 'application/json') ? util.stringify( result.result, result.resOptions.minify, _ ) : result.result );

So if you have your own xml content as string and you set the mime-type to 'application/xml' it will write back to the client. This is the best I can offer now. There is not XML serialization in the package, it is you have to do, when you write your own business function. By calling such serialization functionality before the callback, you might get what you want to possess.

jlg7 commented 9 years ago

@imrefazekas - should the headers take precedence when making the decision to stringify? It seems like you'll stringify based on the rest functions content type, https://github.com/imrefazekas/connect-rest/blob/master/lib/connect-rest.js#L140.

If one overrides the headers content type or omits in the rest function, then the content type will always be application/json. This is fine in case of omission; however, if one sets the headers content type "text/csv", then the results will be stringified. It seems like the rest functions content type designation cascades down by design and defaults to application/json. If that was true, then you'd overwrite the headers of the rest function instead of checking for presence and honoring. If there is an easy way to do this using this module, then please me know. The use case would be accepting a format path or query parameter via the rest function and making the content type decision within a single rest function.

Let me know if you have any questions. @jongeho1