In a recent article I talked about developing an ASP.NET Core Web API Application on macOS using Visual Studio Code. I also developed a quick Python Script that used the Web API. However, to test the ASP.NET Core Web API I first used PyCharm's REST Client to make sure everything was working accordingly.
PyCharm's REST Client
I am still learning PyCharm as I learn Python. Everyday I find something new and wonderful about it. Today I took advantage of it's REST Client to fully test my ASP.NET Core Web API example. Here is a look at the ASP.NET Core Web API Controller from my previous article.
using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using RemindersSvc.Services; namespace RemindersSvc.Contollers { [Route("api/[[controller]")] public class RemindersController : Controller { private readonly IMyReminders myReminders; public RemindersController(IMyReminders reminders) { myReminders = reminders; } [HttpGet("1234")] public IEnumerable<string> Get() { return myReminders.FetchAll(); } [HttpPost] public void Post([FromBody]string reminder) { myReminders.Add(reminder); } [HttpDelete] public void Remove([FromBody]string reminder) { myReminders.Remove(reminder); } } }
I needed to test the HTTP GET, HTTP POST, and HTTP DELETE methods of my Web API to make sure they correctly list, add, and remove reminders. PyCharm's REST Client to the rescue, since I was using PyCharm to develop the Python Script that consumes the ASP.NET Core Web API Example.
Below is a screenshot showing PyCharm's REST Client.
First, I set the Host/Port of the application. By default when developing ASP.NET Core Web API Applications on macOS using Kestrel as the web server, the Host/Port of the application will be http://localhost:5000.
Next, I set the path. In my Web API I am using the path api/reminders.
Finally, I run the HTTP GET request and see the list of reminders being returned from the ASP.NET Core Web API.
PyCharm's REST Client has full support for performing POSTs, PUTs, DELETEs, etc. I can send text, send text from a file, send parameters, multipart/form-data, etc.
Once I fully tested and verified the ASP.NET Core Web API Application worked successfully with PyCharm's REST Client, I was able to develop the Python Script knowing the server-side code was functioning properly. It's simple code, but knowing the Web API was working correctly helped me know that any issues on the client-side were due to my code or incorrect use of the Requests Python HTTP Library.
import requests def display_all_reminders(): r = requests.get('http://localhost:5000/api/reminders') reminders = r.json() for reminder in reminders: print reminder print '----------' # get reminders display_all_reminders() # add reminder requests.post('http://localhost:5000/api/reminders', json='Exercise') display_all_reminders() # remove reminder requests.delete('http://localhost:5000/api/reminders', json='Exercise') display_all_reminders()
Meditate Eat a nutritious breakfast Relax with a cup of tea Be thankful Make the most of my day ---------- Meditate Eat a nutritious breakfast Relax with a cup of tea Be thankful Make the most of my day Exercise ---------- Meditate Eat a nutritious breakfast Relax with a cup of tea Be thankful Make the most of my day ----------
I am getting more and more comfortable with Python and ASP.NET Core each and everyday. I hope you find these articles useful. I can be found on Twitter as @KoderDojo.