POST method receiving object vázio. C#

Asked

Viewed 1,270 times

0

I have a controller with a POST method that always receives the null parameter. I’m sending JSON through Portman.

I have tried to put the class as parameter, I have tried for string as parameter and in both cases is leaving it null;

Follows the controller:

using API_Shop.AppComponents.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace API_Shop.Controllers
{
    public class TesteController : ApiController
    {
        // GET: api/Teste
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/Teste/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/Teste
        public void Post([FromBody]string value)
        {
            Teste tt = new Teste();
            tt = JsonConvert.DeserializeObject<Teste>(value);
            throw new Exception(tt.Texto.ToString());
        }

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

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

This Exception is giving 'Value cannot be null'. The other test stating the type of the 'Test' parameter is also receiving null. In this case, the Exception that is launched is of null object reference.

Follows JSON sent by POSTMAN:

{
  "Teste":{
  "texto":"teste de post para controller"
   }
}

Already test the GET and it works normally.

Can someone give me a hand with this? How do I get the object correctly?

  • Where’s the code from where you’re doing the post?

  • From what I understand of your question you want to know where the POST is coming from. I am sending by POSTMAN. I go to headers put the Content-type in application/json and describe the file in the Body tab and give 'send'

  • This is in Asp.net core?

2 answers

1

Well, you are sending a JSON "object" as your Action expecting a string

Content of the POST

{
  "Teste":{
  "texto":"teste de post para controller"
   }
}

Sua Action

// POST: api/Teste
public void Post([FromBody]string value)

When you do this, you’re performing the POST of JSON representing a Objeto who owns another Objeto, called Teste, which in turn possesses the atributo "text", where the valor "test post to controller". Meanwhile in your Action you simply expect a string.

The correct representation for this submission would be to create objects to reflect their structure and "type" their input parameter Action

public PostApresentado
{
   public Teste Teste {get;set;}
}    

public Teste
{
   public string Texto {get; set;}
}

Your Action should expect the following input:

public void Post([FromBody]PostApresentado value)
{
}

And working that way, you don’t need JsonConvert to serialize and deserialize, this is the responsibility of the framework and you can work "directly" with the objects/classes.

  • @ABCDEFGH, it was clear to you??

-3

If you do not need it to be a string you can receive the model or DTO. I see that you then convert the string to your "model" Test object. I think you have the same problem that I have. It works in the app, but not in Postman. Did you solve it? I also need to send a string instead of json and then convert as well as you. The previous Post didn’t realize your problem, but I did.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.