Search code examples
httpie

storing httpie output with colors in document


I would like to use httpie to generate documentation for a REST web services. The idea would be to have a text containing sample requests with comments

'ping the server','http -v get :8080/ping'
'submit document','http -v post :8080/document name=asdf' 

A script would then execute the requests and capture the nicely formatted output in a document.

Is there a way to do that?


Solution

  • You could also use Pygments CLI (pip install pygments). That should provide cleaner HTML, and it also gives you the option to choose any from the many Pygments styles.

    {
    
    # 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
    

    Output:

    enter image description here

    <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">&quot;hello&quot;</span><span class="p">:</span> <span class="s2">&quot;world&quot;</span><span class="p">}</span>
    </pre></div>