你的位置:首页 > ASP.net教程

[ASP.net教程]Postman测试Web API

 

如何查看查询字符串?→输入包含查询字符串的URL,比如:http://localhost:54176/api/ProductCategories?name=darren&age=25
→点击Params,查询字符串以键值队集合的形式存储
→选中键值对,右键选择"EncodeURIComponent"可以对键或值加密
如何使用Body?→form-data选项,输入表单数据
→x-www-form-urlencoded选项,输入表单数据,Postman自动加密
→raw选项,输入json数据{
    "name":"",
    "username":""
    "age":25
}
→binary选项,上传图片、音频、视频等这里准备了一些API需要测试:GET     /categories
POST    /categories
PUT     /categories/{id}
GET     /categories/{id}
DELETE  /categories/{id}POST    /categories/{id}/products
GET     /categories/{id}/products
DELETE  /categories/{id}/products/{productId}
GET     /categories/{id}/products/{productId}
PUT     /categories/{id}/products/{productId}
GET请求测试?→点击No environment创建环境变量host: http://localhost
port:8080→GET请求GET {{host}}:{{port}}/categories
GET {{host}}:{{port}}/categories/1
GET {{host}}:{{port}}/categories/1/productsPOST请求测试?■ 创建cateogory==请求POST {{host}}:{{port}}/categoriesHeaders:Content-Type: application/jsonBody:{
    "name":""
}==响应status 201 created{
    "id":"1",
    "name":"",
    "products":[]
}■ 创建product
==请求POST {{host}}:{{port}}/categories/1/productsHeaders:Content-Type: application/jsonBody:{
    "name":"",
    "features":["h","o"]
}==响应status 201 created{
    "id":"1",
    "name":"",
    "features":["h","o"]
}
PUT请求测试?==请求PUT {{host}}:{{port}}/categories/1Headers:Content-Type: application/jsonBody:{
    "name":""
}==响应status 200 ok{
    "message":""
}
DELET请求测试?
==请求DELETE {{host}}:{{port}}/categories/1
==响应{
    "message":""
}
Query parameters请求字符串测试?==请求GET {{host}}:{port}/categories/1/products?name=h==响应status 200 ok[
    {
        "id":"1",
        "name":"h",
        "features":["h",...]
    },
    {
        "id":"1",
        "name":"h",
        "features":["h",...]
    }    
]验证授权?验证解决是谁的问题。授权解决能做什么的问题。通常的情形是:→ 输入用户名和密码POST .../auth/login
Headers:Content-Type: application/jsonBody:{
    "login":"",
    "password":""
}→ 服务端返回token在响应中获取到token→ 再次请求在Headers中放入tokenGET ../categoriesHeaders:token:.....
Content-Type: application/json