Reputation: 183
I have a angularjs function which is calling a server side url and getting responses. This server side some times returning a xml response. sometimes returning a blog as the response. Can't predict. If I set the response type to "blob" in client side, it is showing image properly. But I cannot handle non blob responses because of xml responses also showing as a blob. Searched a lot in web about converting blob to xml(response containing a xml response). But couldn't find an answer :(
If I remove the response type: "blob", can handle the non blob response but cannot handle blob response.
Is there a way to handle both type of responses in a single method?
this is my angularjs code for getting response
$http({
url: apiConstants.BASE_URL + 'login',
method: "POST",
responseType: "blob",
data: {
"Req": req
},
headers: {
'X-Username': aUser,
'X-Password': aPass,
"Content-Type": "application/xml"
},
dataType: "xml"
}).success(function(data, status) {
console.log(data);
}
Upvotes: 4
Views: 1984
Reputation: 183
I solved this issue by doing following changes
$http({
url: apiConstants.BASE_URL + 'login',
method: "POST",
responseType: "blob",
data: {
"Req": req
},
headers: {
'X-Username': aUser,
'X-Password': aPass,
"Content-Type": "application/xml"
},
dataType: "xml"
}).success(function(data, status, headers, config) {
var responseType = headers('Content-Type');
if(responseType === 'application/xml;charset=utf-8'){
alert("xml");
}else{
alert("blob");
}
}
Upvotes: 0
Reputation: 6608
If you can manage to set the Content-Type
header correctly in the response while serving your blob and xml responses from your server, then you can use the headers()
in success callback to handle them accordingly.
$http.post(url, postData)
.then(function(response){
// success callback
var responseType = response.headers('Content-Type');
var actualType = responseType.split(';');
if(actualType[0] === 'application/xml'){
// handle XML files here
}else{
// handle blob files here
}
}, function(error){
// error callback
});
Upvotes: 3