Sunil
Sunil

Reputation: 21406

How to set contentType in angularjs $http.get

I have following code in angular for calling a Web API that returns an array of bytes. I would like to set the ContentType to 'application/pdf', which is very simple to do in a jQuery ajax call, but cannot find a way to do this in angularjs.

Question: How would I do this for code below in angularjs?

  return $http.get('/api/v1/alarms/exportsummarytopdf?start=2011-03-
     30T05:00:00.000Z&end=2015-04-14T05:00:00.000Z')
    .then(getSummaryPdfExportComplete)
           .catch(function (message) {
               exception.catcher('XHR failed for getSummaryPdfExport')(message);
           });

        function getSummaryPdfExportComplete(response) {
            logger.info('getSummaryPdfExport: complete');
            return response.data;
        }

UPDATE 1:

When I go to a similar URL duirectly, then Chrome clearly shows content-type as 'application/pdf'. May be I need to use something else when calling a web api that returns an array of bytes.

enter image description here

The error displayed in Chrome is as below. enter image description here

The headers for this failed request, when I use responseType: 'arraybuffer' is as below. enter image description here

Upvotes: 2

Views: 16369

Answers (1)

David Pisoni
David Pisoni

Reputation: 3437

Short answer to the basic question is if you want to set a header in your $http.get() call, send it as part of the "config" (the second, optional parameter.)

E.g.:

$http.get('/api/v1/alarms/exportsummarytopdf?start=2011-03-
  30T05:00:00.000Z&end=2015-04-14T05:00:00.000Z',
  {headers: { 'Accept': 'application/pdf' });

To your specific question, it looks like you want "Accept" and not "Content-Type".

See HTTP headers for more info.

Upvotes: 5

Related Questions