forked from Kong/httpsnippet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
52 lines (43 loc) · 1.37 KB
/
Copy pathclient.ts
File metadata and controls
52 lines (43 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @description
* HTTP code snippet generator for Java using Unirest.
*
* @author
* @shashiranjan84
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
import { CodeBuilder } from '../../../helpers/code-builder';
import { Client } from '../../targets';
export const unirest: Client = {
info: {
key: 'unirest',
title: 'Unirest',
link: 'http://unirest.io/java.html',
description: 'Lightweight HTTP Request Client Library',
},
convert: ({ method, allHeaders, postData, fullUrl }, options) => {
const opts = {
indent: ' ',
...options,
};
const { join, push } = new CodeBuilder({ indent: opts.indent });
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
if (!methods.includes(method.toUpperCase())) {
push(
`HttpResponse<String> response = Unirest.customMethod("${method.toUpperCase()}","${fullUrl}")`,
);
} else {
push(`HttpResponse<String> response = Unirest.${method.toLowerCase()}("${fullUrl}")`);
}
// Add headers, including the cookies
Object.keys(allHeaders).forEach(key => {
push(`.header("${key}", "${allHeaders[key]}")`, 1);
});
if (postData.text) {
push(`.body(${JSON.stringify(postData.text)})`, 1);
}
push('.asString();', 1);
return join();
},
};