如何使用typed-rest-client设置请求标头?

时间:2018-12-24 17:58:41

标签: typescript typed-rest-client

我想使用Typescript中的typed-rest-client向请求添加一些标头。

我没有找到任何文档,所以我稍微看了typed-rest-client源代码,但是我没有完全了解它的工作原理。

我尝试了以下代码,但出现错误:“ ReferenceError:标头未定义”。因此,我认为不必创建Headers类的实例。

import * as restm from 'typed-rest-client';
...
var restc: restm.RestClient = new restm.RestClient(
  'api.openweathermap.org',
  'https://api.openweathermap.org',
);
var temperaturePath = '/data/2.5/weather';
var apiKey = 'xxx';
var headrs = new Headers();
headrs.append('appid', apiKey);
headrs.append('q', location);
headrs.append('units', 'metric');
var requestOptions = {
  additionalHeaders: headrs
};
const response = restc.get(temperaturePath, requestOptions);

1 个答案:

答案 0 :(得分:0)

您需要使用下面所述的AppKernel.php

config.yml

还有IRequestOptions

export interface IRequestOptions {
    // defaults to application/json
    // common versioning is application/json;version=2.1
    acceptHeader?: string,
    // since accept is defaulted, set additional headers if needed
    additionalHeaders?: ifm.IHeaders,

    responseProcessor?: Function,
    //Dates aren't automatically deserialized by JSON, this adds a date reviver to ensure they aren't just left as strings
    deserializeDates?: boolean
}

示例代码:

IHeaders