Monday, February 19, 2018

Get string content posted to a web api

If you have to post string content to a web api you can use the below example. [FromBody] attribute will work only for formatted content like json or xml.


  [Route("GetData")]
        [HttpPost]
        public virtual async System.Threading.Tasks.Task<HttpResponseMessage> GetData()
        {
            string someData = await Request.Content.ReadAsStringAsync();
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            return response;
        }


The syntax is similar to the JS async await - http://deebujacob.blogspot.com/2018/02/here-is-simple-example-on-how-to-use.html

No comments: