forked from luckymarmot/Paw-PythonRequestsCodeGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.mustache
60 lines (56 loc) · 1.86 KB
/
python.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
# Install the Python Requests library:
# `pip install requests`
import requests
{{#body.has_json_body}}
import json
{{/body.has_json_body}}
def send_request():
# {{{request.name}}}
# {{{request.method}}} {{{url.base}}}
try:
response = requests.{{{method}}}(
url="{{{url.base}}}",
{{#url.has_params}}
params={
{{#url.params}}
"{{{name}}}": "{{{value}}}",
{{/url.params}}
},
{{/url.has_params}}
{{#headers.has_headers}}
headers={
{{#headers.header_list}}
"{{{header_name}}}": "{{{header_value}}}",
{{/headers.header_list}}
},
{{/headers.has_headers}}
{{#body.has_raw_body}}
data="{{{body.raw_body}}}"
{{/body.has_raw_body}}
{{#body.has_long_body}}
data="" # set your body string
{{/body.has_long_body}}
{{#body.has_url_encoded_body}}
data={
{{#body.url_encoded_body}}
"{{{name}}}": "{{{value}}}",
{{/body.url_encoded_body}}
},
{{/body.has_url_encoded_body}}
{{#body.has_multipart_body}}
files={
{{#body.multipart_body}}
"{{{name}}}": "{{{value}}}",
{{/body.multipart_body}}
},
{{/body.has_multipart_body}}
{{#body.has_json_body}}
data=json.dumps({{{body.json_body_object}}})
{{/body.has_json_body}}
)
print('Response HTTP Status Code: {status_code}'.format(
status_code=response.status_code))
print('Response HTTP Response Body: {content}'.format(
content=response.content))
except requests.exceptions.RequestException:
print('HTTP Request failed')