From 7d8f4722ae8164d4cf86d6216a62148f31168c6e Mon Sep 17 00:00:00 2001 From: thc202 Date: Fri, 6 Oct 2023 16:30:12 +0100 Subject: [PATCH] Do not parse OTHER endpoints as JSON Override the default Python code sample to only parse JSON for JSON endpoints, otherwise print the content of the response. Fix #12. Signed-off-by: thc202 --- package.json | 2 +- widdershins/templates/code_python.dot | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 widdershins/templates/code_python.dot diff --git a/package.json b/package.json index 8871f86..56531d9 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "zap-api-docs", "version": "1.0.0", "scripts": { - "build": "widdershins --search false --language_tabs 'shell:Shell' 'java:Java' 'python:Python' --summary openapi.yaml source/includes/apis.md" + "build": "widdershins -u widdershins/templates/ --search false --language_tabs 'shell:Shell' 'java:Java' 'python:Python' --summary openapi.yaml source/includes/apis.md" }, "dependencies": { "widdershins": "^3.6.7" diff --git a/widdershins/templates/code_python.dot b/widdershins/templates/code_python.dot new file mode 100644 index 0000000..2c1f52b --- /dev/null +++ b/widdershins/templates/code_python.dot @@ -0,0 +1,10 @@ +import requests +{{?data.allHeaders.length}}headers = { +{{~data.allHeaders :p:index}} '{{=p.name}}': {{=p.exampleValues.json}}{{?index < data.allHeaders.length-1}},{{?}} +{{~}}} +{{?}} +r = requests.{{=data.method.verb}}('{{=data.url}}'{{? data.requiredParameters.length}}, params={ +{{~ data.requiredParameters :p:index}} '{{=p.name}}': {{=p.exampleValues.json}}{{? data.requiredParameters.length-1 != index }},{{?}}{{~}} +}{{?}}{{?data.allHeaders.length}}, headers = headers{{?}}) + +{{?data.url.includes("/JSON/")}}print(r.json()){{?? true }}print(r.content){{?}}