Sourabh Devpura
Sourabh Devpura

Reputation: 79

Why axios not submitting data to asp.core api

I am trying to send data from React app to my asp.net core API by the help of Axios but not getting the data,

Axios Code

    axios.put(urlPath,
                {
                        method: 'put',
                        data: {
 PageDbId: "Soursbh",
            UserDbId:""
}
                    })
                .then(response => onSuccess(response))
                .catch(e => onError(e));

Asp Core Model Class

public class WebAvatarModel
    {
        public string PageDbId { get; set; }

        public string UserDbId { get; set; }

    }

Asp core Code

[HttpPut]
    public IActionResult Save_Avatar([FromBody] WebAvatarModel model)
            {
                var users = model;
    
    
                return null;
            }

Always getting null in PageDbId and UserDbId { get; set; }

I also tried this

await axios.put(urlPath, JSON.stringify(body),
                {
                    method: 'put',
                    headers: {'Content-Type': 'application/json'}
                })
            .then(response => onSuccess(response))
            .catch(e => onError(e));


 [HttpPut]
        public async Task<IActionResult> Save_Avatar(WebAvatarModel model)
        {
            var users = model;


            return null;
        }

But still not working What I am doing wrong?

Upvotes: 0

Views: 937

Answers (2)

Fei Han
Fei Han

Reputation: 27803

Always getting null in PageDbId and UserDbId

If you check the actual request payload using browser developer tool, you would find that the data look like below.

enter image description here

You need to modify your code as below to provide correct request payload.

axios.put(urlPath,
    {
        PageDbId: "Soursbh",
        UserDbId: ""
    })
    .then(response => onSuccess(response))
    .catch(e => onError(e));

Or

axios({
    method: 'put',
    url: urlPath,
    data: {
        PageDbId: "Soursbh",
        UserDbId: ""
    }
})
    .then(response => onSuccess(response))
    .catch(e => onError(e));

Test Result

enter image description here

Upvotes: 1

Dai Nguyen
Dai Nguyen

Reputation: 79

public IActionResult Save_Avatar([FromBody] WebAvatarModel model)

Did this api have [HttpPut]?

Upvotes: 2

Related Questions