-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcharles_to_postman_27.py
246 lines (224 loc) · 8.82 KB
/
charles_to_postman_27.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""
charles_to_postman.py
by: Amber Race
python version: 2.7
Converts the *.chlsj output of Charlesproxy to the Postman json format.
Charles Schema:
[
{
"method": "POST",
"protocolVersion": "HTTP/1.1",
"scheme": "https",
"host": "www.example.com",
"port": null,
"path": "/foo",
"query": null,
"request": {
"sizes": {
"headers": 556,
"body": 67
},
"mimeType": "application/json",
"charset": "UTF-8",
"contentEncoding": null,
"header": {
"firstLine": "POST /foo HTTP/1.1",
"headers": [
{
"name": "Connection",
"value": "keep-alive"
},
{
"name": "Content-Length",
"value": "67"
}
]
},
"body": {
"text": "{\"key\":\"value\"}",
"charset": "UTF-8"
}
},
"response": {
"status": 200,
"sizes": {
"headers": 0,
"body": 281
},
"mimeType": "application/json",
"charset": "UTF-8",
"contentEncoding": null,
"header": {
"firstLine": "HTTP/1.1 200 OK",
"headers": [
{
"name": "content-type",
"value": "application/json; charset=UTF-8"
},
{
"name": "content-length",
"value": "281"
}
]
},
"body": {
"text": "{\"the\":\"response\"}",
"charset": "UTF-8"
}
}
}
]
Postman Schema:
{
"info": {
"description": "Converted from <input_file>",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item":[
{
"name": "/foo",
"request": {
"url": "https://www.example.com/foo",
"method": "POST",
"header": [
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Content-Length",
"value": "67"
}
],
"body": {
"mode": "raw",
"raw": "{\"key\":\"value\"}"
}
},
"response": [
{
"name": "/foo",
"originalRequest": {
"url": "https://www.example.com/foo",
"method": "POST",
"header": [
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Content-Length",
"value": "67"
}
],
"body": {
"mode": "raw",
"raw": "{\"key\":\"value\"}"
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"_postman_previewtype": "parsed",
"header": [
{
"name": "content-type",
"value": "application/json; charset=UTF-8"
},
{
"name": "content-length",
"value": "281"
}
],
"body": "{\"the\":\"response\"}"
}
]
}
]
}
"""
import json
import os
import argparse
def convert_charles_to_postman(charles_node):
"""Converts the request/response info from Charles into a Postman item"""
postman_item = {}
protocol = charles_node['protocolVersion']
path = charles_node['path']
method = charles_node['method']
postman_item['name'] = path
if charles_node['port']:
url = '%s://%s:%s%s' % (charles_node['scheme'],
charles_node['host'], charles_node['port'], path)
else:
url = '%s://%s%s' % (charles_node['scheme'],
charles_node['host'], path)
if charles_node['query']:
url = "%s?%s" % (url, charles_node['query'])
c_request = charles_node['request']
p_request = {'url': url, 'method': method, 'header': [],
'body': {'mode': 'raw', 'raw': ''}, 'description': ''}
for header in c_request['header']['headers']:
p_request['header'].append(
{'key': header['name'], 'value': header['value']})
if c_request['sizes']['body'] > 0:
body = {'mode': 'raw', 'raw': c_request['body']['text']}
p_request['body'] = body
postman_item['request'] = p_request
c_response = charles_node['response']
postman_item['response'] = []
status_code = c_response['status']
p_response = {'name': path, 'originalRequest': p_request,
'code': status_code, 'header': [], 'cookie': []}
try:
p_status = c_response['header']['firstLine'].replace(
'%s %d ' % (protocol, status_code), '')
p_response['status'] = p_status
except KeyError:
pass
for header in c_response['header']['headers']:
p_response['header'].append(
{'key': header['name'], 'value': header['value']})
if c_response['sizes']['body'] > 0:
p_response['body'] = c_response['body']['text']
if c_response['mimeType'] == 'application/json':
p_response['_postman_previewlanguage'] = 'json'
p_response['_postman_previewtype'] = 'parsed'
postman_item['response'].append(p_response)
return postman_item
def get_json_dict_from_input_file(input_file):
"""Extracts json string from input file and converts to a dict"""
if not os.path.isfile(input_file):
print "File does not exist: ", input_file
return
input_string = open(input_file, "r").read()
try:
chls_json = json.loads(input_string)
except ValueError, value_err:
print "File does not contain valid json", value_err
return chls_json
if __name__ == '__main__':
PARSER = argparse.ArgumentParser(
description='Charlesproxy to Postman converter')
PARSER.add_argument(
'-i', '--input', help='Input file in the *.chlsj format', required=True)
PARSER.add_argument('-o', '--output', help='Output file', required=True)
PARSER.add_argument(
'-n', '--name', help='Name of the target Postman collection', required=True)
ARGS = PARSER.parse_args()
CHARLES_JSON = get_json_dict_from_input_file(ARGS.input)
if CHARLES_JSON:
POSTMAN_JSON = {
"info": {
"name": ARGS.name,
"description": "Converted from " + ARGS.input,
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": []
}
for CHARLES_NODE in CHARLES_JSON:
POSTMAN_JSON['item'].append(
convert_charles_to_postman(CHARLES_NODE))
OUTPUT_FILE = open(ARGS.output, 'w')
OUTPUT_FILE.write(json.dumps(POSTMAN_JSON))
OUTPUT_FILE.flush()
OUTPUT_FILE.close()