0

Please find the below code which I used to send the bytes in HTTPCLIENT.

   public void SendProtoBufBytes(byte[] payload)
    {         

            #region WebAPI Call              

            using (HttpClient client = new HttpClient())
            {
                string baseUrl = ConfigurationManager.AppSettings["Environment"];
                client.BaseAddress = new Uri(baseUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//For BINARY files - ("Content-type", "application/octet-stream");

                var byteArrayContent = new ByteArrayContent(payload);

                HttpResponseMessage response = client.PostAsync("api/processwebjobs/processjobrequest", byteArrayContent).Result;

                if (response.IsSuccessStatusCode)
                {
                    string processServerResponse = response.Content.ReadAsStringAsync().Result;
                }

           }

            #endregion
        }

Here, payload is a ProtoBuf file which is converted into Bytes.

WebApi Method:

[RoutePrefix("api/processwebjobs")]
public class ProcessWebJobsApiController : BaseApiController
{
    #region Public Api Methods

    [HttpPost]
    [Route("processjobrequest")]
    public IHttpActionResult ProcessJobRequest([FromBody] byte[] payload)
    {
    }
 }

Appreciate your help!

Vignesh
  • 814
  • 7
  • 29
  • 3
    You’ll want to POST it most likely, not try to put it into the query string. Especially if it may be a bit larger amount. Or you’ll need to encode it first if you really need to use GET. – Sami Kuhmonen Nov 10 '17 at 11:23
  • @SamiKuhmonen Thanks for your quick response. Let me change to httppost and try. – Vignesh Nov 10 '17 at 11:25
  • @SamiKuhmonen I have tried with the HTTPPOST, But Still no luck. I have updated the Question to post method. – Vignesh Nov 10 '17 at 11:33
  • 1
    Does https://stackoverflow.com/questions/32184360/post-byte-array-to-web-api-server-using-httpclient help? – mjwills Nov 10 '17 at 12:43
  • @mjwills that link is helped! thank you, guys! – Vignesh Nov 30 '17 at 09:18

0 Answers0