M
M
Mikhail Plyusnin2016-08-11 09:28:59
.NET
Mikhail Plyusnin, 2016-08-11 09:28:59

Web Api method parameter(POST) was coming null?

Make a project with React.js + Redux on the client and WebApi.Net on the backend, the problem is that for some reason the parameters of the POST request from the client:

//webApiConfigure.js
...
if (process.env.NODE_ENV !== 'production') {
    instance = axios.create({
        baseURL: 'http://localhost:2270/api/',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    });
}
...
//signupform.js
import api from '../webApiConfigure'
...
api.post('/Message/', {
            text: '121321',
            AuthorTwitterHandle: '3123123',
        })
        .then((response) => {
            console.log(response);
        });
...

Come to the backend Controller in a method with NULL :
[EnableCors("*", "*", "*")]
    public class MessageController : ApiController
    {
        public HttpResponseMessage Post([FromBody]ChatMessage message) //пробовал без FromBody, без толку
        {
            if (message == null || !ModelState.IsValid)
            {
                return Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest,
                    "Invalid input");
            }
            messages.Add(message);
            return Request.CreateResponse(HttpStatusCode.Created);
        }
    }

bbd5d1f9776c46539ba60251bd64f83f.pngActually, how to treat it and how to live with it? I’ve been sitting for the second day already ... And in the example of one person, as I understand it, his parameters are easily bound to the ChatMessage model

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Plyusnin, 2016-08-11
@rpe4a

The problem was solved, at random ... deleted the line

headers: { 'Content-Type': 'application/x-www-form-urlencoded' }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question