返回二进制数据的模拟响应

对于某些请求,可能需要使用二进制数据(如文档或图像)进行响应。

在开发代理中,可以通过设置 response.body 字符串值来定义二进制响应,该值以 @ 相对于当前工作目录的文件路径开头,例如:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/mockresponseplugin.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。

示例

另请参阅相关的开发代理示例: