概览
目标: 将二进制文件作为模拟响应返回
时间: 5 分钟
Plugins:MockResponsePlugin
先决条件:设置开发代理
对于某些请求,可能需要使用二进制数据(如文档或图像)进行响应。
在开发代理中,你可以通过将 response.body 设置为以 @ 开头的字符串值,后跟相对于模拟文件的文件路径来定义二进制响应,例如:
文件: mocks.json
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.3.1/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/users/*/photo/$value",
"method": "GET"
},
"response": {
"body": "@picture.jpg",
"headers": [
{
"name": "content-type",
"value": "image/jpeg"
}
]
}
}
]
}
当你调用 GET https://graph.microsoft.com/v1.0/users/ben@contoso.com/photo/$value 时,你会获得存储在当前目录中 picture.jpg 文件中的图像。
注意
如果你在使用命令行执行 HTTP 请求,请确保已将 dollar 字符正确转义。 请参阅 代理为何不模拟我的二进制响应。
下一步
详细了解 MockResponsePlugin。
示例
另请参阅相关的开发代理示例: