import uncurl cmd ="""curl -X POST \ http://test10.ak.xyz/sc/data/mws/orders \ -H 'Authorization: Bearer 2c49uM5Qx20bhug1NazNor8iRU4jtcV5UjGX02d/Bfypm8H7hPsiLyL/fTaQaLc65njTzE+dUh1eBkbyK6diAVOKBIpxFsKtoxtCaUkrrKKDMb8' \ -H 'Content-Type: application/json' \ -H 'Postman-Token: 3d610c70-cadf-4e27-938d-ece460f0422b' \ -H 'cache-control: no-cache' \ -d '{ "sid":"A9C30D6AC35693", "start_date":"2021-05-22 14:08:57", "end_date":"2021-05-22 14:08:58" }'""" cxt = uncurl.parse_context(cmd ) print({**cxt.headers},cxt.url,cxt.data,cxt.cookies,sep='\n')
pip install uncurl
swagger to json :
import swagger2 import unittest import requests import os import warnings import swagger2 from swagger2 import utils class APITestCase(unittest.TestCase): default_file = 'file.txt' @classmethod def setUpClass(cls): warnings.simplefilter('ignore',ResourceWarning) # 准备测试资源 if not os.path.exists(cls.default_file): with open(cls.default_file, mode='w') as f: f.write('Hello world!') url = 'https://petstore.swagger.io/v2/swagger.json' cls.swagger = swagger2.parse(url,verify=False) @classmethod def tearDownClass(cls): # 清理测试资源 if os.path.exists(cls.default_file): os.remove(cls.default_file) def test_apis(self): for api in self.swagger.apis: with self.subTest(api.get('name')) as st: # 请求地址 url = api.get('url') # 请求方法 method = api.get('method') # 请求头 headers = api.get('headers') # 路径参数 paths = api.get('paths') # 查询字串,即query string params = api.get('query') # 普通表单,即 Content-Type = application/x-www-form-urlencoded data = api.get('form') # 文件表单, 即 Content-Type = multipart/form-data formData = api.get('formData') # json格式的参数, 即 Content-Type = application/json payload = api.get('json') # 文件上传时建议用requests框架的请求头 if headers.get('Content-Type') == 'multipart/form-data': del headers['Content-Type'] # 路径参数格式化 url = utils.path_format(url, paths) # 文件表单参数格式化 formData = utils.form_format(formData) res = requests.request(method=method, url=url, headers=headers, params=params, data=data, files=formData, json=payload, timeout=30, verify=False) print(res.text) self.assertTrue(res.ok) def api_to_json(): import json import swagger2 # url = 'https://petstore.swagger.io/v2/swagger.json' url = 'http://172.18.23.223:8000/swagger.json' swagger = swagger2.parse(url) print('转换接口:{}个'.format(len(swagger.apis))) api_path = 'api2.json' with open(api_path, mode='w', encoding='utf8') as f: f.write(json.dumps(swagger.apis, ensure_ascii=False)) if __name__ == '__main__': api_to_json()