divshot / ask

A simple, chainable way to construct HTTP requests in Node or the browser.
MIT License
4 stars 1 forks source link

parse response based on content-type #10

Open scottcorgan opened 9 years ago

mbleigh commented 9 years ago

Maybe look for application/json but also have an explicit option e.g. parse('json') with an optional parsing function like parse(function(res){ return res.body.toLowerCase() })

scottcorgan commented 9 years ago

(as a note):

var getSomething = request
  .get('something')
  .parse('json');

getSomething().then(function(jsonResponse) {

});
mbleigh commented 9 years ago

I think the default case should be to parse application/json response type, but everything else leave alone. Then you can explicitly say parse('text') to disable the parsing even if content type is application/json.

scottcorgan commented 9 years ago

Agreed.

mbleigh commented 9 years ago

Ok, one more thought and I'm done. Maybe it would make sense to be able to register parsers for different content types? So I could register a text/html parser or a special application/json+vnd.whatever parser? Not sure.

scottcorgan commented 9 years ago

like plugins?

var request = ask({
  origin: 'http://someapi.com',
  parsers: {
    'some-parser-name': require('../parsers/some-parser.json')
  }
});

request.get('some-endpont').parse('some-parser-name');
mbleigh commented 9 years ago

Not necessarily called that, but not far off. I'd suggest:

var request = ask({
  origin: 'http://someapi.com',
  parsers: {
    html: {
      contentType: "text/html",
      parse: function(res){ /* ... */ }
    }
  }
});

request.get('some-endpont').parse('some-parser-name');

If a contentType option is specified, anything of that content type will automatically be parsed. This should be able to be a string or regexp. The users parsers should be merged into a default set (which is maybe just 'json').

If the user passes parse(false) to the chain, that should disable any parsing. Similarly, parse(true) should enable content-type based parsing (but not prefer a specific one). parse('parser-name') should mandate that regardless of content-type, parse using the specified parser, and parse(function(res){ }) should be possible for an on-the-fly parser.

I think that covers everything, anything I'm missing?

scottcorgan commented 9 years ago

That covers it.

contentType wont' be required, but, if it is included, that parser will only act on content-types specified in that field and ignore any content-types that don't match that, correct?

mbleigh commented 9 years ago

contentType is a way to specify an automatic triggering of the parsing. If it's present, you should still be able to manually call it to force that parser's usage.

On Thu Nov 06 2014 at 12:28:13 PM Scott Corgan notifications@github.com wrote:

That covers it.

contentType wont' be required, but, if it is included, that parser will only act on content-types specified in that field and ignore any content-types that don't match that, correct?

— Reply to this email directly or view it on GitHub https://github.com/divshot/ask/issues/10#issuecomment-62045601.