WebAPI generates data that can be imported to PostMan, webapipostman

Source: Internet
Author: User

WebAPI generates data that can be imported to PostMan, webapipostman
I. Preface

Currently, it is very common to use webapis as a service-oriented enterprise. It is undeniable that webapis are easy to use. You can generate corresponding help documents (Microsoft. aspNet. webApi. helpPage), but it is more convenient and does not have the corresponding integration with the official support for persistent debugging, although we can use methods such as Fiddler, Swagger, PostMan, and other handwritten code to debug our WebAPI, it is still not very convenient, for the company, there must also be a standardized, persistent, and effective specification.

I hope the debugging interface can reach the following three points:

1: Test data can be generated according to the interface definition.

2: Test data can be persisted to facilitate continuous iteration.

3: test data can be shared. This document mainly describes how to use PostMan to implement these three points.

Ii. debugging methods

Currently, the following debugging methods are available:

1: The familiar Fiddler is not enough. It is very powerful, but it is not what we want.

2: PostMan, easy to use and satisfied with the above 2 and 3, this is a Google plug-in (address here), there are also App version (https://www.getpostman.com/) recommended.

3: Swagger, which has been integrated with SwaggerUI into WebAPI and installed on Nuget

install-package Swashbuckle

The Project address is https://github.com/domaindrivendev/swashbuckle.

4: hand-written HttpClient. Others include https://github.com/wuchang/webapitestclient.

PostMan is recommended for the above methods.

3. Basic PostMan operations

Let's take a look at some basic operations on PosMan.

As shown below, we have defined several simple methods as our debugging interface.

Using System; using System. collections. generic; using System. linq; using System. web. http; using WebAPI2PostMan. models; namespace WebAPI2PostMan. controllers {// <summary> /// Product service // </summary> [RoutePrefix ("Product")] public class ProductController: apiController {private static readonly List <Product> Products = new List <Product> {new Product {Id = Guid. newGuid (), Description = "product Description", Name = "Product Name ", Price = 123}, new Product {Id = Guid. newGuid (), Description = "Product Description", Name = "Product Name", Price = 124}, new Product {Id = Guid. newGuid (), Description = "Product Description", Name = "Product Name", Price = 125}, new Product {Id = Guid. newGuid (), Description = "product Description", Name = "Product Name", Price = 126 }}; /// <summary> /// obtain All products /// </summary> [HttpGet, Route ("All")] public IEnumerable <Product> Get () {return Products;} // <summary> // get Obtain the product // </summary> /// <param name = "id"> product no. </param> [HttpGet, Route ("{id}")] public string Get (Guid id) {return "value ";} /// <summary> /// add product /// </summary> /// <param name = "request"> product request </param> [HttpPost, route ("")] public string Post (Product request) {Products. add (request); return "OK ";} /// <summary> /// edit the product /// </summary> /// <param name = "id"> product no. </param> /// <param name = "request"> Edit Product </param> [HttpPut, Route ("{id}")] public void Put (int id, Product request) {}/// <summary> /// Delete the product /// </summary> /// <param name = "id"> product no. </param> [HttpDelete, route ("{id}")] public string Delete (Guid id) {var model = Products. firstOrDefault (x => x. id. equals (id); Products. remove (model); var result = string. format ("Product NO. {0} deleted successfully! ", Id); return result ;}}}

The help document generated after Microsoft. AspNet. WebApi. HelpPage is added to Nuget, as shown in.

Public class PostmanCollection {public string id {get; set;} public string name {get; set;} public string description {get; set;} public List <string> order {get; set;} public long timestamp {get; set;} public List <PostmanRequest> requests {get; set ;}} public class PostmanRequest {public string collection {get; set ;} public string id {get; set;} public string name {get; set;} public string dataMode {get; set;} public List <PostmanData> data {get; set ;} public string description {get; set;} public string descriptionFormat {get; set;} public string headers {get; set;} public string method {get; set ;} public Dictionary <string, string> pathVariables {get; set;} public string url {get; set;} public int version {get; set;} public string collectionId {get; set ;}} public class PostmanData {public string key {get; set ;}public string value {get; set ;}public string type {get {return "text ";}} public bool enabled {get {return true ;}}}

PostMan supports import from the address, so we can define a PostManController to generate our Json data according to the interface definition.

Using System. collections. generic; using System. linq; using System. web. http; using System. web. http. description; using System. web. http. results; using Newtonsoft. json; using WebAPI2PostMan. areas. helpPage; using WebAPI2PostMan. models; namespace WebAPI2PostMan. controllers {// <summary> //// </summary> [RoutePrefix ("PostMan")] public class PostManController: ApiController {private const string Host =" http://localhost:11488/ "; /// <Summary> /// obtain the PostMan set /// </summary> /// <returns> </returns> [Route (" ")] public JsonResult <PostmanCollection> GetPostmanCollection () {var collectionId = PostMan. getId (); var apis = Configuration. services. getApiExplorer (). apiDescriptions. where (x => x. documentation! = Null); var requests = GetPostmanRequests (apis, collectionId); var collection = new PostmanCollection {id = collectionId, name = "WebAPI2PostMan", description = "", order = requests. select (x => x. id ). toList (), timestamp = 0, requests = requests}; return Json (collection);} private List <PostmanRequest> GetPostmanRequests (IEnumerable <ApiDescription> apis, string collectionId) {return apis. select (api => new PostmanRequest {collection = collectionId, id = PostMan. getId (), name = api. documentation, dataMode = "urlencoded", data = GetPostmanDatas (api), description = "", descriptionFormat = "html", headers = "", method = api. httpMethod. method, pathVariables = new Dictionary <string, string> (), url = Host + api. relativePath, version = 2, collectionId = collectionId }). toList ();} private List <PostmanData> GetPostmanDatas (apide1_api) {var postmandatas = new List <PostmanData> (); var apiModel = Configuration. getHelpPageApiModel (api. getFriendlyId (); var raw = apiModel. sampleRequests. values. firstOrDefault (); if (raw = null) return postmandatas; var pdata = JsonConvert. deserializeObject <Dictionary <string, string> (raw. toString (); postmandatas. addRange (pdata. select (model => new PostmanData {key = model. key, value = model. value}); return postmandatas ;}}}

The main generated code is to get all interface and interface-defined parameters. We can use Microsoft. AspNet. WebApi. HelpPage to obtain SampleRequests and use SampleRequests to populate our test data.

Run the program to access http: // localhost: 11488/PostMan and import it to PostMan.

Window. open ("chrome-extension: // fdmmgilgnpjigdojojpjoooidkmcomcm/index.html"); pm. collections. importCollectionFromUrl ("http: // localhost: 11488/postman ");

You can achieve one-click Import. However, it is a pity that it must be called in the plug-in before it is effective, and the code is in a hurry. There are many places to continue to expand, but the purpose of this article has been achieved.

Sample Code: https://github.com/yanghongjie/WebAPI2PostMan

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.