0

I have .NET API Example:

[Route("[controller]")]
[ApiController]
public class UserController : ControllerBase
{
    [HttpGet("GetUser")]
    public ActionResult GetUserDetails(User userReq)
    {
        var response = service.GetUser(userReq);
        return Ok(response);
    }
}

And User class Example:

public class User
{
        public int? ID { get; set; }
        public string Name { get; set; }
        public int? Age { get; set; }
        public char? Gender { get; set; }
        public string Email { get; set; }
        public int? Phone { get; set; }
}

I am calling the API in below way through axios in React, Example:

axios.get(String(APIUrls.GetUser),
{
    params: {
        ID: null,
        Name: null,
        Age: null,
        Gender: null,
        Email: null,
        Phone: null
    },
}).then(response => {
    console.log(response.data);
})
.catch((err) => {
    console.log(err);
});

I am able to get response through postman, but when I try from my application its not working.

Error:

Status Code: 415

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"xxxxxxxxxxxx:00000005"}
4
  • Try adding [Produces("application/json")] over controller class Commented Dec 31, 2021 at 10:47
  • Hi Peter, the API is working fine, I am getting response through postman. Commented Dec 31, 2021 at 11:28
  • It surely may work fine, but it may be worth trying, as it does not change the api output, only header of responses. Commented Dec 31, 2021 at 11:31
  • I tried it, but still getting same error. Commented Dec 31, 2021 at 11:34

1 Answer 1

1

Add [FromQuery] attribute to specify the source:

[HttpGet("GetUser")]

public ActionResult GetUserDetails([FromQuery] User userReq)
{
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.