gpt4 book ai didi

typescript - 如何使用 typed-rest-client 设置请求 header ?

转载 作者:行者123 更新时间:2023-12-05 04:02:58 34 4
gpt4 key购买 nike

我想在 Typescript 中使用 typed-rest-client 向请求添加一些 header 。

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

我尝试了以下代码,但出现错误:“ReferenceError: Headers is not defined”。所以我认为我不必创建 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);

最佳答案

您需要使用下面说明的 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

export interface IHeaders { [key: string]: any };

示例代码:

let options: IRequestOptions = {additionalHeaders: {'key' : 'value' }};
let rest: rm.RestClient = new rm.RestClient(param1, param2, param3, options);
...

关于typescript - 如何使用 typed-rest-client 设置请求 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53916469/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com