1

I am trying to learn Web API and created my first project as below. I am testing it using postman. The post method works fine and I get response message – but the input received at the controller for the post action is null. What need to be done to get the post value in the controller?

using System.Collections.Generic;
using System.Net.Http;
using System.Web.Http;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        List<Comment> comments;

        // GET api/values
        public IEnumerable<Comment> Get()
        {
            return comments;
        }

        // GET api/values/5
        public Comment Get(int id)
        {
            Comment c = comments[id-1];

            if (string.IsNullOrEmpty(c.Description))
            {
                throw new HttpResponseException(System.Net.HttpStatusCode.NotFound);
            }

            return c;
        }

        // POST api/values
        public HttpResponseMessage Post(Comment inputComment)
        {
            Comment c = new Comment();
            if (inputComment != null)
            {
                c.Description = inputComment.Description;
                c.ID = inputComment.ID;
            }
           //var response = new HttpResponseMessage(HttpStatusCode.Created);
           //return response;

            var response = Request.CreateResponse<Comment>(System.Net.HttpStatusCode.Created, c);
            response.Headers.Location=new System.Uri(Request.RequestUri,"/api/values/"+c.ID.ToString());
            return response;
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }

        public ValuesController()
        {
            comments = new List<Comment>();

            Comment comment1 = new Comment();
            comment1.ID = 1;
            comment1.Description = "Test1";

            Comment comment2 = new Comment();
            comment2.ID = 2;
            comment2.Description = "";

            comments.Add(comment1);
            comments.Add(comment2);

        }
    }
}

POSTMAN Request/Response

enter image description here

POSTMAN Request Header enter image description here

UPDATE

After using 'raw' in the request body, it worked fine. In POSTMAN, when I clicked "Generate Code", it is now displaying correct headers.

enter image description here

1
  • Add [HttpPost] ontop of the method and [FromBody] before the attribute Comment Commented Jul 17, 2016 at 10:37

1 Answer 1

5

Use Raw as body type instead of Form-data and input you JSON.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.