我有一个大型API文档,每个请求都有相同的请求标头,如Accept: application/json
和Cookies: SessionID
。有没有办法可以全局声明这些以避免重复?
答案 0 :(得分:1)
我申请的最常见的事情是定义一个数据结构,然后在所有这些请求中使用它。就我而言,我将它用于授权标题,它们都是相同的。
数据结构的示例及其用法(无论您实际使用它的位置,请求的标题或正文,在这种情况下都在正文中):
FORMAT: 1A
HOST: http://polls.apiblueprint.org/
# Auth API
This is an Auth API, where you can obtain authentication/authorization for your app in our system.
It is necessary that you provide your credentials in order to use the API. The endpoints of the Auth API are only to obtain a valid access token, which would be provided along with each call.
# Group Authentication
## Get a request token [/auth/request]
Obtain a request token so you can exchange it for an access token.
Requests tokens have a short expiry rate, and are only one time use.
### GET
+ Request (application/json)
+ Attributes
- Authorization (OAuth Request)
+ Response 200 (application/json)
{
"oauth_token" : "request-token-ng7805hg85hjt89gu258ty25",
"oauth_token_secret" : "TOKEN SECRET TO BE USED WHILE SIGNING REQUESTS"
}
## Exchange a request token for an access token [/auth/exchange]
Once you have got a request token, you will be able to trade it for an access token and then be able to call the different API endpoints.
### GET
+ Request (application/json)
+ Attributes
- Authorization (OAuth Exchange)
+ Response 200 (application/json)
{
"oauth_token" : "AN_INACTIVE_FRESH_ACCESS_TOKEN"
}
# Data Structures
## OAuth base (object)
+ oauth_consumer_key: YOUR_CONSUMER_KEY (string, required)
+ oauth_nonce: A_UNIQUE_TOKEN_FOR_THIS_REQUEST_GENERATED_BY_YOU (string, required)
+ oauth_signature: A_SIGNATURE_HASH_BASED_ON_THE_REQUEST_PARAMS (string, required)
+ oauth_signature_method: `HMAC-SHA256` (string, required)
+ oauth_timestamp: MILLISECONDS_FROM_EPOC_UNIX_TIME (string, required)
+ oauth_version: 1.0 (string, required)
## OAuth Request (object)
+ Include OAuth base
+ oauth_callback: YOUR_CALLBACK_URL (string, required)
## OAuth Exchange (object)
+ Include OAuth base
+ oauth_token: YOUR_RECENTLY_OBTAINED_REQUEST_TOKEN (string, required)
您只需要在括号之间添加数据结构的名称,就是这样。您可以根据需要重复使用它。即使构建其他数据结构,如果需要,您也希望将其他数据结构建立在先前定义的数据结构上。