0

I have a JS app which makes requests to a server which can return results in JSON or CSV depending on the Accept header. I'd like for the user to be able to click on a button and download the CSV to the user's hard drive.

Is there some way I can make a request and set the Accept header?

I know there are other ways I can do it (different resource, query parameters), and I don't what I'm describing is possible, but it would be nice to do it 'correctly' if I can.

Joe
  • 46,419
  • 33
  • 155
  • 245

1 Answers1

1

Since you have tagged this xmlhttprequest, you can use setRequestHeader('header name', 'header value'). MDN docs.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • It was tagged `xmlhttprequest` speculatively. Will using it allow the the downloading of the resource? – Joe Aug 05 '14 at 14:56
  • @Joe — I've seen approaches using `data:` URIs that make it possible. There's no other way to make an HTTP request from a browser while specifying the accept header from JS though. – Quentin Aug 05 '14 at 15:00
  • To clarify, when I say 'download' I mean 'download to hard drive' not just 'make a request'. A `data:` URI assumes I've already got the data in the browser. I didn't think it was possible to do precisely what I asked. Looks like that's correct? – Joe Aug 05 '14 at 15:03
  • @Joe — That was my understanding of what you meant when I indicated the `data:` URI solution. – Quentin Aug 05 '14 at 15:04
  • Thanks for your help. The API endpoint needs to exist anyway. I'll just fake it with a query string. – Joe Aug 05 '14 at 15:07