feat: add custom header

This commit is contained in:
moonrailgun 2023-12-04 22:19:05 +08:00
parent 2600e3e6fa
commit addda5c45a
2 changed files with 44 additions and 0 deletions

View File

@ -56,8 +56,34 @@ const MonitorHttp: React.FC = React.memo(() => {
</Select.Option> </Select.Option>
</Select> </Select>
</Form.Item> </Form.Item>
<Form.Item
label="Headers"
name={['payload', 'headers']}
rules={[
{
validator(rule, value, callback) {
try {
const obj = JSON.parse(value);
if (typeof obj !== 'object') {
callback('Not JSON Object');
} else {
callback();
}
} catch {
callback('Not valid JSON string');
}
},
},
]}
>
<Input.TextArea
rows={4}
placeholder='For example:&#13;&#10;{ "key": "value" }'
/>
</Form.Item>
<Form.Item label="Body" name={['payload', 'bodyValue']}> <Form.Item label="Body" name={['payload', 'bodyValue']}>
<Input.TextArea <Input.TextArea
rows={4}
placeholder='For example:&#13;&#10;{ "key": "value" }' placeholder='For example:&#13;&#10;{ "key": "value" }'
/> />
</Form.Item> </Form.Item>

View File

@ -10,6 +10,7 @@ export const http: MonitorProvider<{
method?: string; method?: string;
timeout?: number; // second timeout?: number; // second
contentType?: string; contentType?: string;
headers?: string;
bodyValue?: string; bodyValue?: string;
maxRedirects?: number; maxRedirects?: number;
ignoreTLS?: boolean; ignoreTLS?: boolean;
@ -24,6 +25,7 @@ export const http: MonitorProvider<{
method = 'get', method = 'get',
timeout = 30, timeout = 30,
contentType, contentType,
headers,
bodyValue, bodyValue,
maxRedirects, maxRedirects,
ignoreTLS, ignoreTLS,
@ -44,6 +46,20 @@ export const http: MonitorProvider<{
// }, // },
}; };
if (headers) {
try {
const customHeaders = JSON.parse(headers);
if (typeof customHeaders === 'object') {
config.headers = {
...config.headers,
...customHeaders,
};
}
} catch (err) {
logger.warn('Unabled header:', headers);
}
}
if (bodyValue) { if (bodyValue) {
config.data = bodyValue; config.data = bodyValue;
} }
@ -55,6 +71,8 @@ export const http: MonitorProvider<{
config.httpsAgent = new https.Agent(httpsAgentOptions); config.httpsAgent = new https.Agent(httpsAgentOptions);
console.log('config', config);
try { try {
const startTime = dayjs(); const startTime = dayjs();
const res = await axios({ ...config }); const res = await axios({ ...config });