私はかなり長い間、Loopback v4 パッケージの初期バージョンを使用してきました。RestBindings.Http.Request バインディングは、最新のアップグレードの 1 つまでうまく機能していました (それがいつ行われたかはよくわかりません)。
ほとんどのプロパティの値を取得できますが、クエリ プロパティは取得できません。デフォルトの ping コントローラーを使用する最も基本的なプロジェクトでも、クエリ プロパティは空のままです。以下は、私のコード、クエリ、および応答のサンプルです。
import { Request, RestBindings, get, ResponseObject } from '@loopback/rest';
import { inject } from '@loopback/context';
export class PingController {
constructor(@inject(RestBindings.Http.REQUEST) private req: Request) { }
// Map to `GET /ping`
@get('/ping')
ping(): object {
// Reply with a greeting, the current time, the url, and request headers
return {
query: 'Query response: ' + this.req.query.start,
greeting: 'Hello from LoopBack',
date: new Date(),
url: this.req.url,
headers: Object.assign({}, this.req.headers),
};
}
}
クエリ: localhost:3000/ping?start=2018-08-25&end=2018-09-09&user=larsm
出力:
{
"query": "Query response: undefined",
"greeting": "Hello from LoopBack",
"date": "2018-11-27T23:21:53.142Z",
"url": "/ping?start=2018-08-25&end=2018-09-09&user=larsm",
"headers": {
"host": "localhost:3000",
"connection": "keep-alive",
"cache-control": "max-age=0",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"accept-encoding": "gzip, deflate, br",
"accept-language": "nb-NO,nb;q=0.9,en-GB;q=0.8,en;q=0.7,no;q=0.6,nn;q=0.5,en-US;q=0.4" } }