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/products

POST请求测试?

■ 创建cateogory

==请求

POST {{host}}:{{port}}/categories

Headers:

Content-Type: application/json

Body:

{
    "name":""
}

==响应

status 201 created

{
    "id":"1",
    "name":"",
    "products":[]
}

■ 创建product


==请求

POST {{host}}:{{port}}/categories/1/products

Headers:

Content-Type: application/json

Body:

{
    "name":"",
    "features":["h","o"]
}

==响应

status 201 created

{
    "id":"1",
    "name":"",
    "features":["h","o"]
}


PUT请求测试?

==请求

PUT {{host}}:{{port}}/categories/1

Headers:

Content-Type: application/json

Body:

{
    "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/json

Body:

{
    "login":"",
    "password":""
}

→ 服务端返回token

在响应中获取到token

→ 再次请求在Headers中放入token

GET ../categories

Headers:

token:.....
Content-Type: application/json

posted @ 2016-02-17 13:28  Darren Ji  阅读(2589)  评论(0编辑  收藏  举报

我的公众号:新语新世界,欢迎关注。