Динамически возвращать файл на основе аргумента запроса - PullRequest
0 голосов
/ 24 января 2019

Эта заглушка определена в моем каталоге сопоставлений. Я пытаюсь вернуть файл динамически на основе значения моего параметра запроса (параметр запроса и имя файла будут иметь одинаковое имя)

Пожалуйста, смотрите ниже пример.

{
  "request": {
    "method": "GET",
    "urlPathPattern": "/some/url/locales/en_GB/products",
    "queryParameters": {
      "typeName": {
        "matches": "([A-Za-z]*)"
      }
    }
  },
  "response": {
    "status": 200,
    "bodyFileName" : "{{request.pathSegments.[5]}}",
    "transformers": [
      "response-template"
    ]
  }
}

Запрос GET будет выглядеть примерно так:

.../some/url/locales/en_GB/products?typeName=blah

В моем каталоге __files у меня будет файл с именем blah.json, которому я хочу, чтобы ответ соответствовал.

Я не уверен, что bodyFileName правильный. 5 представляет позиционирование параметра запроса с нулевым индексом.

1 Ответ

0 голосов
/ 24 января 2019

Выяснить, какие значения представлены различными переменными шаблона ответа, не сложно.Ниже приведен пример для g

{
  "request": {
    "method": "GET",
    "urlPathPattern": "/some/url/locales/en_GB/products",
    "queryParameters": {
      "typeName": {
        "matches": "([A-Za-z]*)"
      }
    }
  },
  "response": {
    "status": 200,
    "body" : "request.requestline.pathSegments:\n[0]: {{request.requestLine.pathSegments.[0]}}\n[1]: {{request.requestLine.pathSegments.[1]}}\n[2]: {{request.requestLine.pathSegments.[2]}}\n[3]: {{request.requestLine.pathSegments.[3]}}\n[4]: {{request.requestLine.pathSegments.[4]}}\n[5]: {{request.requestLine.pathSegments.[5]}}\n\nrequest.query.typeName: {{request.query.typeName}}",
    "transformers": [
      "response-template"
    ]
  }
}

И запрос GET http://localhost:8080/some/url/locales/en_GB/products?typeName=test приводит к такому ответу:

request.requestline.pathSegments:
[0]: some
[1]: url
[2]: locales
[3]: en_GB
[4]: products
[5]: 

request.query.typeName: test

С учетом вышесказанного вы можете видеть, что искомое значение равноне извлекается с помощью request.pathSegments[x], а request.query.typeName.В результате получается:

{
  "request": {
    "method": "GET",
    "urlPathPattern": "/some/url/locales/en_GB/products",
    "queryParameters": {
      "typeName": {
        "matches": "([A-Za-z]*)"
      }
    }
  },
  "response": {
    "status": 200,
    "bodyFileName": "{{request.query.typeName}}.json",
    "transformers": [
      "response-template"
    ]
  }
}
...