如何使用 Javascript 为 API 请求格式化我的 headers?
How do I format my headers for an API request using Javscript?
我正在尝试使用具有以下要求的 the Discogs API:
Content-Type: application/x-www-form-urlencoded
Authorization:
OAuth oauth_consumer_key="your_consumer_key",
oauth_nonce="random_string_or_timestamp",
oauth_signature="your_consumer_secret&",
oauth_signature_method="PLAINTEXT",
oauth_timestamp="current_timestamp",
oauth_callback="your_callback"
User-Agent: some_user_agent
我刚刚开始学习 Javascript 并且会使用 fetch。该文档解释了如何很好地使用它——包括如何添加 header。我的问题是,如何格式化正确的 Discogs 要求?
这是我卡住的地方:
const discogs_headers = {
"Content-Type": "application/x-www-form-urlencoded",
...
}
如何将以下内容添加到该词典中?
Authorization:
OAuth oauth_consumer_key="your_consumer_key",
oauth_nonce="random_string_or_timestamp",
oauth_signature="your_consumer_secret&",
oauth_signature_method="PLAINTEXT",
oauth_timestamp="current_timestamp",
oauth_callback="your_callback"
我试过了,但它不能正常工作:
const discogs_headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "OAuth oauth_consumer_key="your_consumer_key", oauth_nonce="random_string_or_timestamp",...
}
注意:我实际上是在那里填写我的申请信息,只是为了这个问题的目的使用文档条目。如果措辞不当,我深表歉意。为了清楚起见,请不要犹豫,问我是否可以添加一些内容。
感谢您的宝贵时间。
您可以像这样在 Authorization
部分使用模板文字(为简单起见缩写)。
const consumerKey = 'your_consumer_key';
const nonce = 'random_string_or_timestamp';
// ...
const discogs_headers = {
'Content-Type': "application/x-www-form-urlencoded",
Authorization: `OAuth oauth_consumer_key="${consumerKey}", oauth_nonce="${nonce}"`
};
console.log(discogs_headers);
显然,consumerKey
、nonce
等是与您的应用关联的值。同样,整个 Authorization
header 没有填写在这个答案中,但它的要点就在那里。
我正在尝试使用具有以下要求的 the Discogs API:
Content-Type: application/x-www-form-urlencoded
Authorization:
OAuth oauth_consumer_key="your_consumer_key",
oauth_nonce="random_string_or_timestamp",
oauth_signature="your_consumer_secret&",
oauth_signature_method="PLAINTEXT",
oauth_timestamp="current_timestamp",
oauth_callback="your_callback"
User-Agent: some_user_agent
我刚刚开始学习 Javascript 并且会使用 fetch。该文档解释了如何很好地使用它——包括如何添加 header。我的问题是,如何格式化正确的 Discogs 要求?
这是我卡住的地方:
const discogs_headers = {
"Content-Type": "application/x-www-form-urlencoded",
...
}
如何将以下内容添加到该词典中?
Authorization:
OAuth oauth_consumer_key="your_consumer_key",
oauth_nonce="random_string_or_timestamp",
oauth_signature="your_consumer_secret&",
oauth_signature_method="PLAINTEXT",
oauth_timestamp="current_timestamp",
oauth_callback="your_callback"
我试过了,但它不能正常工作:
const discogs_headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "OAuth oauth_consumer_key="your_consumer_key", oauth_nonce="random_string_or_timestamp",...
}
注意:我实际上是在那里填写我的申请信息,只是为了这个问题的目的使用文档条目。如果措辞不当,我深表歉意。为了清楚起见,请不要犹豫,问我是否可以添加一些内容。
感谢您的宝贵时间。
您可以像这样在 Authorization
部分使用模板文字(为简单起见缩写)。
const consumerKey = 'your_consumer_key';
const nonce = 'random_string_or_timestamp';
// ...
const discogs_headers = {
'Content-Type': "application/x-www-form-urlencoded",
Authorization: `OAuth oauth_consumer_key="${consumerKey}", oauth_nonce="${nonce}"`
};
console.log(discogs_headers);
显然,consumerKey
、nonce
等是与您的应用关联的值。同样,整个 Authorization
header 没有填写在这个答案中,但它的要点就在那里。