Testing ASP.NET Web API using Postman

by GeeksArray

This tutorial describes how to test using Postman GET, POST, PUT, Delete http methods created by ASP.NET Web API. You can test the API using Fiddler or SOAP UI or Postman. For this tutorial we will use Postman.

Postman features

  1. Postman helps you to test, build and document API faster than any other tool.
  2. You can easily manage environment variables from one API request to other. E.g. if tokenstring result details needs to send to other API method.
  3. It is trusted tool used by PayPal, VMWare, Bing, Box
  4. You can create collection and share with your team members. or Import collection which you or your team member already created.

Follow below steps to use Postman for testing ASP.NET Web API

  1. Install Postman tool

    Download Postman debugging tool and install.

    Once it is installed, open postman tool from Start menu.

  2. Create ASP.NET Core Web API

    You can visit CRUD operations using ASPNET Core and implement CRUD API using ASP.NET Core Web API. It creates a sample application which creates Create, Read, Update and Delete http methods and works with inmemory database to store data.

    Once you have implemented API, you can launch Web API application to see the result of Get method in Browser. Now your APIs are running on Kestrel server and you must be seeing localhost url in browser like http://localhost:<portnumber>/api/supplier.

  3. Test Http Get method using Postman

    Open Postman tool if it is not open. You will see two pane in screen. Left pane shows the history of the Rest URL's which you already used and collection tab using which you can create collection or import collection. And right side pane you can make new request to API and see response.

    From Postman tool - right pane select GET from DropDown, then enter your Web API url like http://localhost:<portnumber>/api/supplier and click on Send button.

    This will make a request to Web API server and get response. You can get response in JSON, XML, HTML, Text format.

    Test Web API Get method using Postman tool

  4. Test HTTP Post with Postman

    In this step you will test HTTP Post Web API method with postman. Web API method JSON object in FromBody. Request will contain JSON object which Web API will convert into Model and create a record into database.

    In postman follow below steps to use Post method.

    1. From dropdown select Post.
    2. Enter your web api service url into text box. In this case it will be like - http://localhost:<port number>/api/supplier
    3. Select Body radio button.
    4. Select Raw
    5. Select Json (application/json)
    6. Enter Json object with Supplier object value
    7. Click on Send
    8. Notice status it shows as 201 Created as response.
    9. As API returns Created Supplier object, it shows in response body of Postman.

    how to use postman to test http post methods

  5. Test HTTP Put with Postman

    HTTP Put is used to Create or Update entity. The differenece between PUT and POST is, PUT is idempotent meaning that you call PUT n number of times, it will only modify existing entity if it is already existing else it will create a new entity. POST is not idempotent meaning if you call POST n number of times it will create new entities n number of times.

    To update existing entity, client needs to provide entire entity details with updated values for all attributes.

    In postman follow below steps to use Put method.

    1. From dropdown select Put.
    2. Enter your web api service url into text box. Use Supplier ID in url. In this case it will be like - http://localhost:<port number>/api/supplier/1
    3. Select Body radio button.
    4. Select Raw
    5. Select JSON(application/json)
    6. Enter Json object with updated supplier model values
    7. Click on Send
    8. If Supplier record successfully gets updated it returns status as 200 Ok
    9. See the output as Supplier Updated Successfully. which is retrun by service as Content Result.

    test aspnet webapi http put using postman.

  6. Test HTTPDelete with Postman

    HTTPDelete method is responsible to delete existing entity from data store. If HTTPDelete request has entity body the body is ignored. So you can send the entity body with request however it has no use.

    If Delete requests found required entity does not exist it returns 404 - Not Found response code. If requested entity exists it deletes from data store and return 200 - Ok as Http Response, if response has content describing status.
    It returns 204 - No Content if response does not have any content.

    In postman follow below steps to use Delete method.

    1. From dropdown select Delete.
    2. Enter your web api service url into text box. Use Supplier ID in url. In this case it will be like - http://localhost:<port number>/api/supplier/1
    3. Click on Send
    4. If Supplier record successfully gets deleted it returns status as 200 Ok as ASP.NET Web API service returns content.

    test asp.net web api httpdelete method using postman

Speak your mind
Please login to post your comment!