locked
Simple Post FromBody example RRS feed

  • Question

  • User518677258 posted

    Tried 3 different methods and each throws error with  Object reference not set to an instance of an object.   How is it done ??  Thanks !!!

    public class ProdData

    {

    public string productName { get; set; }

    public int productID { get; set; }

    }

    public IHttpActionResult Post([FromBody] ProdData prod, string majorLtr, bool useTestQueue)

    {

    try

    {

    // method a -----------------------

    HttpContent requestContent = Request.Content;

    prod = requestContent.ReadAsAsync<ProdData>().Result;

    return Ok(prod.productName);  // throws error

    //  method b ---------------------------------------

    string jsonContent = "waiting for values";

    // jsonContent = requestContent.ReadAsStringAsync().Result;

    // ProdData jsonModel2 = JsonConvert.DeserializeObject<ProdData>(jsonContent);

    //return Ok(jsonModel2.productName);

    // method c

    ProdData jsonModel = new ProdData();

    prod= requestContent.ReadAsAsync<ProdData>().Result;

    return Ok(jsonModel.productName);

    Saturday, December 15, 2018 5:10 AM

All replies

  • User-474980206 posted

    simplest is:

    public IHttpActionResult Post([FromBody] ProdData prod, string majorLtr, bool useTestQueue)
    {
       return Ok(prod.productName);
    }

    Sunday, December 16, 2018 8:50 PM
  • User36583972 posted

    Hi andrewcw,

    Tried 3 different methods and each throws error with  Object reference not set to an instance of an object.   How is it done ??  Thanks !!!

    From your error message, you need to check if the [FromBody] ProdData prod is a null value.

    Then, you need to check the code which you have uesd to the API methods. You can share it with us.

    Usually, we can use the HttpClient PostAsync method to pass parameters and get a response.


    Best Regards,

    Yong Lu

    Monday, December 17, 2018 9:54 AM
  • User518677258 posted

    Hi and thanks for both replies....  Note I am using Framework 4.7.1 instead of Core because my functions will be writing to MSMQ

    I am puzzled because here is the input from Swagger ....

    curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
    "productName": "testName", \
    "productID": 1 \
    }' 'http://localhost:59756/BuyOff/NormalBuyOff'

    And here is the revised simple code

    public IHttpActionResult PostAsync([FromBody] ProdData prod)
    {
    string error = string.Empty;
    string retVal = string.Empty;
    try
    {

    if (prod ==null)
    {
    Console.WriteLine("says null but is this a valid check ?");    //  YES why NULL ?

    }
    HttpContent requestContent = Request.Content;
    prod = requestContent.ReadAsAsync<ProdData>().Result;  // error
    return Ok(prod.productName);

    string jsonContent = "waiting for values";
    // jsonContent = requestContent.ReadAsStringAsync().Result;
    // ProdData jsonModel2 = JsonConvert.DeserializeObject<ProdData>(jsonContent);
    //return Ok(jsonModel2.productName);
    ProdData jsonModel = new ProdData();
    prod= requestContent.ReadAsAsync<ProdData>().Result;
    return Ok(jsonModel.productName);
    }
    catch (Exception e)
    {
    error = "Error Loading to Queues..." + e.Message;
    return Content(HttpStatusCode.BadRequest, error);
    }

    return Ok(retVal);

    }

    Monday, December 17, 2018 4:52 PM
  • User518677258 posted

    If I run a simple asp.net core 2.2 example with swagger the FromBody value is loaded and not NULL.  Yet both CURL show the data  is provided.  Any ideas ?

    Monday, December 17, 2018 8:54 PM
  • User475983607 posted

    If Swagger works then I assume there is a problem with your CURL configuration.  See the CURL docs for assistance.  I build a quick test and HttpClient as well as PostMan worked perfectly.

            static void Main(string[] args)
            {
    
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("http://localhost:57823");
    
                ProdData data = new ProdData()
                {
                    productID = 1,
                    productName = "Hello World"
                };
    
                string json = JsonConvert.SerializeObject(data);
    
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                var result = client.PostAsync("http://localhost:57823/api/default", content).Result;
                
    
                Console.WriteLine(result.Content.ReadAsStringAsync().Result);
    
            }
        }
            // POST: api/Default
            [HttpPost]
            public ProdData Post([FromBody] ProdData model)
            {
                return model;
            }
        public class ProdData
        {
            public string productName { get; set; }
            public int productID { get; set; }
        }

    Monday, December 17, 2018 10:21 PM
  • User-474980206 posted

    in your curl command:

    url -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
    "productName": "testName", \ 
    "productID": 1 \ 
    }' 'http://localhost:59756/BuyOff/NormalBuyOff'

    the \<newline> syntax is only valid on linux and os/x, or in the new bash shell for windows 10.

    Thursday, December 20, 2018 12:17 AM