I have web api which returns binary file, which is consumed by Angular 12 client. Application is deployed on Dev and Integ environments. Now suprisingly, same api is working on Dev but not on Integ. When on Integ, instead of returning binary file, it returns JSON. The only difference is Dev IIS Server (hosting API) version is 8.5 while that of Integ is 10.0.
I have also added necessary response header like :
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
Below is the sample response returned on Integ :
{
"Version": {
"_Major": 1,
"_Minor": 1,
"_Build": -1,
"_Revision": -1
},
"Content": {
"Headers": [
{
"Key": "Content-Type",
"Value": [
"application/octet-stream"
]
}
]
},
"StatusCode": 200,
"ReasonPhrase": "OK",
"Headers": [],
"RequestMessage": null,
"IsSuccessStatusCode": true
}
Below is my API code :
[HttpPost]
[Route("api/Chart/test")]
public async Task<HttpResponseMessage> test(TestEntity request)
{
try
{
var ms = await _service.testMethod(request.Data, request.ExportType, request.FileName);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(ms.ToArray())
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
catch (Exception)
{
var result = new HttpResponseMessage(HttpStatusCode.InternalServerError);
return result;
}
}
And below is my angular method code :
angularTestMethod(request) {
return this.http.post(`${this.apiUrl}api/Chart/test`, request, { withCredentials: true, responseType: 'blob'})
.pipe(catchError(err => this.handleError(err)));
}
Any idea what could be the issue? As code of angular and web api is same for both environments.
Below are the version details:
Angular : Angular 12,
Web API : ASP.Net Web API 2.2
.Net Framework : 4.6.1
Dev Windows Server and IIS : 2012 and 8.5 resp
Integ Windows Server and IIS : 2016 and 10.0 resp
As asked in the comment, Below is another example of issue I am getting :
The TestEntity Class is a complex request object. Rather I will provide another method giving me same issue. Its returning JSON instead of Binary File.
[HttpGet, Route("api/Chart/download/{id}")]
public HttpResponseMessage Download(int id)
{
try
{
var ms = _chartService.Download(id);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(ms.ToArray())
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
catch (FileNotFoundException)
{
var result = new HttpResponseMessage(HttpStatusCode.NotFound);
return result;
}
catch (Exception)
{
var result = new HttpResponseMessage(HttpStatusCode.InternalServerError);
return result;
}
}
And below is Download method :
public MemoryStream Download(int id)
{
var chart= _repository.GetById(id);
if (chart != null)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(chart.MetaData);
writer.Flush();
stream.Position = 0;
return stream;
}
throw new FileNotFoundException("Chart not found");
}
Chart Class :
public class Chart
{
public int Id { get; set; }
public string Name { get; set; }
public string MetaData { get; set; }
public int? ChartFolderId { get; set; }
public int? SortOrder { get; set; }
public bool IsPublic { get; set; }
public bool InActive { get; set; }
public string Owner { get; set; }
public string InActiveBy { get; set; }
public DateTime? InActiveOn { get; set; }
public string InsertedBy { get; set; }
public DateTime InsertedOn { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedOn { get; set; }
}