我想使用httpie生成REST Web服务的文档。我们的想法是拥有一个包含带注释的样本请求的文本
'ping the server','http -v get :8080/ping'
'submit document','http -v post :8080/document name=asdf'
然后,脚本将执行请求并捕获文档中格式良好的输出。
有办法吗?
答案 0 :(得分:1)
我不知道如何通过httpie创建它,但有一种方法可以将格式化输出从bash获取到html:请参阅this question或使用HTML::FromANSI perl模块或{{ 3}}工具。有很多类似的工具,选择最适合你的。
答案 1 :(得分:1)
您也可以使用Pygments CLI(pip install pygments
)。这应该提供更清晰的HTML,并且它还允许您从许多Pygments样式中选择任何一种。
{
# Stylesheet:
echo '<style>'
pygmentize -S default -f html
echo '</style>'
# Request HTTP headers as HTML:
http --print=H httpbin.org/post hello=world | pygmentize -f html -l http /dev/stdin
# JSON request body as HTML:
http --print=B httpbin.org/post hello=world | pygmentize -f html -l json /dev/stdin
} > request.html
输出:
<style>
…
</style>
<div class="highlight"><pre><span class="nf">POST</span> <span class="nn">/post</span> <span class="kr">HTTP</span><span class="o">/</span><span class="m">1.1</span>
<span class="na">Content-Length</span><span class="o">:</span> <span class="l">18</span>
<span class="na">Accept-Encoding</span><span class="o">:</span> <span class="l">gzip, deflate</span>
<span class="na">Accept</span><span class="o">:</span> <span class="l">application/json</span>
<span class="na">User-Agent</span><span class="o">:</span> <span class="l">HTTPie/0.8.0</span>
<span class="na">Host</span><span class="o">:</span> <span class="l">httpbin.org</span>
<span class="na">Content-Type</span><span class="o">:</span> <span class="l">application/json; charset=utf-8</span>
</pre></div>
<div class="highlight"><pre><span class="p">{</span><span class="nt">"hello"</span><span class="p">:</span> <span class="s2">"world"</span><span class="p">}</span>
</pre></div>