Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have the following controller method:

C#
[HttpPost]
[Route("SomeRoute")]
  public byte[] MyMethod([FromBody] string ID)
{
  byte[] mybytearray=db.getmybytearray(ID);//working fine,returning proper result.
  return mybytearray;
}

Now in the calling method(thats also another WebApi method!),I have written like this:

C#
private HttpClient client=new HttpClient ();
        private HttpResponseMessage response=new HttpResponseMessage ();
 byte[] mybytearray= null;
  response = client.GetAsync(string.Format("api/ABC/MyMethod/{0}", ID)).Result;
  if (response.IsSuccessStatusCode)
  {
    mybytearray= response.Content.ReadAsByteArrayAsync().Result;//Here is the problem
   } 

Now, the problem is the byte arrayMyMethod is sending is of 528 bytes, but here after making ReadAsByteArrayAsync, the size becomes larger(706 bytes) and the values are also goofed up. Kind of banging my head against it, any help will be appreciated.

Thanks
Posted

1 solution

Got the solution:
instead of
C#
mybytearray= response.Content.ReadAsByteArrayAsync().Result;

I am doing:
C#
string result=null;
result = response.Content.ReadAsStringAsync().Result.Replace("\"", string.Empty);
 mybytearray=Convert.FromBase64String(result);


The key point is:
It was returning the byte array as base64encoded.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900