How to make an ASP.NET Core Post API without using MVC?

Asked

Viewed 163 times

0

I don’t work with ASP.NET Core Apis until I need it for a project written in C#. So I created with a standard project, with the following controller:

[Route("api/[controller]")]
[ApiController]
public class MetadataProcessController : ControllerBase {
    ...
    ...

    // GET api/MetadataProcess/5
    [HttpGet("{filename}")]
    public ActionResult<string> Get(string filename)
    {
        return "Hello " + filename;
    }

    // POST api/MetadataProcess
    [HttpPost]
    public ActionResult<string> Post([FromBody] string value)
    {
        return "Hello " + value;
    }

    ...
    ...
}

Making call using Get through the URL /api/MetadataProcess/algumaCoisa the result obtained is Hello algumaCoisa in the browser.

However, by calling Post using Postman, encoding the following entry: inserir a descrição da imagem aqui

Response is a Json error:

inserir a descrição da imagem aqui

I tried to insert other values into value, but nothing went right. What could I be doing wrong? I would like to receive in Sponse a Hello + what was sent in the request.

2 answers

1

The payload sent must be a string and not an object. That is to say:

"C%..."

Instead of

{
   value: "C%..."
}

1


The problem is that you are sending a json object, but you are asking for a string in the method. If you want to receive an object, create a class with the Value property:

public class Input
{
    public string Value { get; set; }
}

If you just want a string, actually send a string:

1]

Browser other questions tagged

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