/
java.mustache
85 lines (72 loc) · 2.28 KB
/
java.mustache
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.IOException;
import org.apache.http.client.fluent.*;
{{#body}}
import org.apache.http.entity.ContentType;
{{/body}}
{{#body.has_multipart_body}}
import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.HttpMultipartMode;
{{/body.has_multipart_body}}
public class SendRequest
{
public static void main(String[] args) {
sendRequest();
}
private static void sendRequest() {
// {{{request.name}}} ({{{request.method}}} {{{url.base}}})
try {
{{! ----- }}
{{#body.has_multipart_body}}
HttpEntity entity = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
{{#body.multipart_body}}
.addTextBody("{{{name}}}", "{{{value}}}")
{{/body.multipart_body}}
.build();
{{/body.has_multipart_body}}
// Create request
Content content = Request.{{{method}}}("{{{url.fullpath}}}")
{{#headers.has_headers}}
// Add headers
{{#headers.header_list}}
.addHeader("{{{header_name}}}", "{{{header_value}}}")
{{/headers.header_list}}
{{/headers.has_headers}}
{{! ----- }}
{{#body.has_raw_body}}
// Add body
.bodyString("{{{body.raw_body}}}", ContentType.DEFAULT_TEXT)
{{/body.has_raw_body}}
{{! ----- }}
{{#body.has_long_body}}
// Add body
.bodyString("set your body string", ContentType.DEFAULT_TEXT)
{{/body.has_long_body}}
{{! ----- }}
{{#body.has_url_encoded_body}}
// Add body
.bodyForm(Form.form()
{{#body.url_encoded_body}}
.add("{{{name}}}", "{{{value}}}")
{{/body.url_encoded_body}}
.build())
{{/body.has_url_encoded_body}}
{{! ----- }}
{{#body.has_multipart_body}}
// Add body
.body(entity)
{{/body.has_multipart_body}}
{{! ----- }}
{{#body.has_json_body}}
// Add body
.bodyString("{{{body.json_body_object}}}", ContentType.APPLICATION_JSON)
{{/body.has_json_body}}
// Fetch request and return content
.execute().returnContent();
// Print content
System.out.println(content);
}
catch (IOException e) { System.out.println(e); }
}
}