mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-21 22:59:07 +00:00
support form-urlencoded
This commit is contained in:
parent
544bbb2d2f
commit
57c13add9c
@ -243,11 +243,32 @@ export default class API {
|
||||
}
|
||||
|
||||
try {
|
||||
const finalHeaders: Dictionary<string> = {
|
||||
...apiHeaders,
|
||||
...headers,
|
||||
};
|
||||
|
||||
let finalBody:
|
||||
| JSONObject
|
||||
| JSONArray
|
||||
| URLSearchParams
|
||||
| undefined = data;
|
||||
|
||||
// if content-type is form-url-encoded, then stringify the data
|
||||
|
||||
if (
|
||||
finalHeaders['Content-Type'] ===
|
||||
'application/x-www-form-urlencoded' &&
|
||||
data
|
||||
) {
|
||||
finalBody = new URLSearchParams(data as Dictionary<string>);
|
||||
}
|
||||
|
||||
const result: AxiosResponse = await axios({
|
||||
method: method,
|
||||
url: url.toString(),
|
||||
headers: { ...apiHeaders, ...headers },
|
||||
data,
|
||||
headers: finalHeaders,
|
||||
data: finalBody,
|
||||
});
|
||||
|
||||
result.headers = await this.onResponseSuccessHeaders(
|
||||
|
@ -20,49 +20,68 @@ router.get(
|
||||
res: ExpressResponse,
|
||||
next: NextFunction
|
||||
): Promise<void> => {
|
||||
try {
|
||||
logger.info('Request Headers: ');
|
||||
logger.info(req.headers);
|
||||
logger.info('Request Body: ');
|
||||
logger.info(req.body);
|
||||
|
||||
const responseCode: number | undefined =
|
||||
LocalCache.getNumber('TestServer', 'responseCode') || 200;
|
||||
const responseTime: number | undefined =
|
||||
LocalCache.getNumber('TestServer', 'responseTime') || 0;
|
||||
const responseBody: string | undefined =
|
||||
LocalCache.getString('TestServer', 'responseBody') || '';
|
||||
let responseHeaders: JSONValue | undefined =
|
||||
LocalCache.getJSON('TestServer', 'responseHeaders') || {};
|
||||
|
||||
logger.info('Response Code: ' + responseCode);
|
||||
logger.info('Response Time: ' + responseTime);
|
||||
logger.info('Response Body: ');
|
||||
logger.info(responseBody);
|
||||
logger.info('Response Headers: ');
|
||||
logger.info(responseHeaders);
|
||||
|
||||
if (responseHeaders && typeof responseHeaders === Typeof.String) {
|
||||
responseHeaders = JSON.parse(responseHeaders.toString());
|
||||
}
|
||||
|
||||
if (responseTime > 0) {
|
||||
await Sleep.sleep(responseTime);
|
||||
}
|
||||
|
||||
// middleware marks the probe as alive.
|
||||
// so we don't need to do anything here.
|
||||
return Response.sendCustomResponse(
|
||||
req,
|
||||
res,
|
||||
responseCode,
|
||||
responseBody,
|
||||
responseHeaders ? (responseHeaders as any) : {}
|
||||
);
|
||||
} catch (err) {
|
||||
return next(err);
|
||||
}
|
||||
await returnResponse(req, res, next);
|
||||
}
|
||||
);
|
||||
|
||||
router.post(
|
||||
'/',
|
||||
async (
|
||||
req: ExpressRequest,
|
||||
res: ExpressResponse,
|
||||
next: NextFunction
|
||||
): Promise<void> => {
|
||||
await returnResponse(req, res, next);
|
||||
}
|
||||
);
|
||||
|
||||
const returnResponse: Function = async (
|
||||
req: ExpressRequest,
|
||||
res: ExpressResponse,
|
||||
next: NextFunction
|
||||
): Promise<void> => {
|
||||
try {
|
||||
logger.info('Request Headers: ');
|
||||
logger.info(req.headers);
|
||||
logger.info('Request Body: ');
|
||||
logger.info(req.body);
|
||||
|
||||
const responseCode: number | undefined =
|
||||
LocalCache.getNumber('TestServer', 'responseCode') || 200;
|
||||
const responseTime: number | undefined =
|
||||
LocalCache.getNumber('TestServer', 'responseTime') || 0;
|
||||
const responseBody: string | undefined =
|
||||
LocalCache.getString('TestServer', 'responseBody') || '';
|
||||
let responseHeaders: JSONValue | undefined =
|
||||
LocalCache.getJSON('TestServer', 'responseHeaders') || {};
|
||||
|
||||
logger.info('Response Code: ' + responseCode);
|
||||
logger.info('Response Time: ' + responseTime);
|
||||
logger.info('Response Body: ');
|
||||
logger.info(responseBody);
|
||||
logger.info('Response Headers: ');
|
||||
logger.info(responseHeaders);
|
||||
|
||||
if (responseHeaders && typeof responseHeaders === Typeof.String) {
|
||||
responseHeaders = JSON.parse(responseHeaders.toString());
|
||||
}
|
||||
|
||||
if (responseTime > 0) {
|
||||
await Sleep.sleep(responseTime);
|
||||
}
|
||||
|
||||
// middleware marks the probe as alive.
|
||||
// so we don't need to do anything here.
|
||||
return Response.sendCustomResponse(
|
||||
req,
|
||||
res,
|
||||
responseCode,
|
||||
responseBody,
|
||||
responseHeaders ? (responseHeaders as any) : {}
|
||||
);
|
||||
} catch (err) {
|
||||
return next(err);
|
||||
}
|
||||
};
|
||||
|
||||
export default router;
|
||||
|
Loading…
Reference in New Issue
Block a user