calling REST API

Anjali Agarwal 1,531 Reputation points
2023-09-23T05:03:04.4733333+00:00

I am trying to call a REST API on my local host for testing purposes. This is what I have in my controller:

 public class SMSController : ControllerBase
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        public readonly IConfiguration _configuration;
        private readonly ITwilioRestClient _client;
        public SMSController(IWebHostEnvironment webHostEnvironment, IConfiguration configuration, ITwilioRestClient client)
        {
            _webHostEnvironment = webHostEnvironment;
            _configuration = configuration;
            _client = client;
        }
        [HttpGet]
        public IActionResult SendSMS(SmsMessage model)
        {
           
            var message = MessageResource.Create(
            to: new PhoneNumber(model.To),
            from: new PhoneNumber(model.From),
            body: model.Message,
            client: _client); // pass in the custom client
            return Ok("Success");
        }
    }
}

This is what I have in the Model class:

    public class SmsMessage
    {
        public string To { get; set; }
        public string From { get; set; }
        public string Message { get; set; }
    }

when I run my application. I append SMS at the end like this:

https://localhost:7162/SMS

I have a breakpoint at SendSMS method, the debugger does not break at this method. How can I call SendSMS method and pass the parameters to test the API.

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 1,510 Reputation points
    2023-09-23T09:34:22.99+00:00

    I have a breakpoint at SendSMS method, the debugger does not break at this method. How can I call SendSMS method and pass the parameters to test the API.

    The browser is displaying a HTTP status 415 (Unsupported Media Type). The reason is an HTTP GET sends data in the URL.

    415 Unsupported Media Type

    There are a couple of way to pass the data depending on your general design intent. The [FromQuery] attribute tells the server to expect data in the URL querystring.

    [HttpGet]
    public IActionResult SendSMS([FromQuery]SmsMessage model)
    {
        return Ok(model);
    }
    
    https://localhost:7250/api/SMS?To=123&From=456&Message=Hello
    
    

    Route parameters are another option. Keep in mind, there is a limit to the URL length so, as mentioned above, an HTTP Post is probably a better approach for sending an SMS message.

    Learn Web API REST fundamentals and the testing tools that come with Web API.

    https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-7.0&tabs=visual-studio

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Mykyta Pavlenko 5 Reputation points
    2023-09-23T07:18:37.6633333+00:00

    Hello!

    I see 2 possible problems with the provided code snippet:

    1. You should have used POST method: [HttpGet] -> [HttpPost] Then whenever you call your endpoint, you have to pass the SMS parameters as http POST request body.
    2. Endpoint route on the controller might be case-sensitive. You can specify the endpoint route directly like [Route("SMS")] or more convenient way to do so - [Route("[controller]")] Controller value will be evaluated based on the controller name convention

    You can also use HTTP repl tool for testing, see https://learn.microsoft.com/en-us/aspnet/core/web-api/http-repl/?view=aspnetcore-7.0&tabs=windows for more details

    1 person found this answer helpful.
    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.