Can AngularJS set the responseType after a response has been received?
<p>I have an Angular 1.x application that is expecting to receive a binary file download (pdf) using a <code>$http.post()</code> call.  The problem is, I'd like to alternatively get a processing error message that's sent as json. I can do this with the config</p>

<pre><code>headers: {
  'Accept': 'application/pdf, application/json'
}
</code></pre>

<p>The problem is I have have to set <code>responseType: 'arraybuffer'</code>, otherwise the pdf binary is escaped (or altered such that it doesn't load).  However, that prevents the json from being read or interpreted correctly.</p>

<p>How can I have both?</p>

<p><strong>Edit:</strong> I'm going to try to clarify; perhaps my understanding is incorrect.</p>

<pre><code>$http({
    method: 'POST',
    url: "/myresource",
    headers: {
        'Accept': 'application/pdf, application/json'
    },
    responseType: 'arraybuffer'
})
.then(
    function(response) {
        // handle pdf download via `new Blob([data])`
    }, function(response) {
        // pop up a message based on response.data
    }
)
</code></pre>

<p>In a scenario where I return a pdf data block and a http status of 200, the first function handles the response and prompts the user to save the file.  However if the status is an error (422), the <code>response.data</code> is undefined.  I assume this is because the <code>responseType</code> is preventing the json from being handled correctly.</p>

<p>If I remove the <code>responseType</code> line, the error data is correctly read, but when the pdf is saved, some of the file bytes aren't correct and it's effectively corrupted.  I assume this is because the file is being encoded because javascript was expecting a string.</p>