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>
</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']}>
<Input.TextArea
rows={4}
placeholder='For example:&#13;&#10;{ "key": "value" }'
/>
</Form.Item>

View File

@ -10,6 +10,7 @@ export const http: MonitorProvider<{
method?: string;
timeout?: number; // second
contentType?: string;
headers?: string;
bodyValue?: string;
maxRedirects?: number;
ignoreTLS?: boolean;
@ -24,6 +25,7 @@ export const http: MonitorProvider<{
method = 'get',
timeout = 30,
contentType,
headers,
bodyValue,
maxRedirects,
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) {
config.data = bodyValue;
}
@ -55,6 +71,8 @@ export const http: MonitorProvider<{
config.httpsAgent = new https.Agent(httpsAgentOptions);
console.log('config', config);
try {
const startTime = dayjs();
const res = await axios({ ...config });