<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:small">On Fri, Jan 31, 2025 at 1:16 AM William Park via kwlug-disc <<a href="mailto:kwlug-disc@kwlug.org" target="_blank">kwlug-disc@kwlug.org</a>> wrote:</div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi all, (also posted to GTALUG)<br>
<br>
To build HTTP request, you use 'curl'.<br>
<br>
After you get HTTP response, what utility do you use to extract stuffs <br>
you want?  JSON format has 'jq', and XML format has 'xmlstarlet'.  I'm <br>
looking for something like that but for HTTP format.<br>
<br>
I know it's simple (HTTP response has 3 parts: first line, headers, and <br>
body), but I just want to reduce chance of typos.<br></blockquote><div><br></div><div style="font-size:small" class="gmail_default">Do you want to do this from the bash, in shell scripts?</div><div style="font-size:small" class="gmail_default">Or something else.</div><div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">From shell scripts, I do this:</div><div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">curl -s -k -L -i -o $TMP -H "User-Agent: $UA" "$URL" 2>&1 <br><br># Print the headers<br>sed -ne '1,/^^M$/p' $TMP</div><div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">That ^M is actually a Ctrl-M which separates the header portion</div><div style="font-size:small" class="gmail_default">from the returned content.</div><div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">For <a href="http://kwlug.org" target="_blank">kwlug.org</a> for example, it returns this</div><div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">HTTP/1.1 301 Moved Permanently<br>Date: Fri, 31 Jan 2025 14:04:01 GMT<br>Server: Apache<br>X-Content-Type-Options: nosniff<br>Location: <a href="http://kwlug.org/" target="_blank">http://kwlug.org/</a><br>Cache-Control: max-age=31536000<br>Expires: Sat, 31 Jan 2026 14:04:01 GMT<br>Content-Length: 225<br>Content-Type: text/html; charset=iso-8859-1 <br></div><div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">That of course means the headers and the content are merged in <br></div><div style="font-size:small" class="gmail_default">one output file, and you have to extract what you need with lots</div><div style="font-size:small" class="gmail_default">of code.<br></div><div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">If it is something more complex, then I use Python's requests library.</div><div style="font-size:small" class="gmail_default">You need to use exceptions to handle things like timeouts and too </div><div style="font-size:small" class="gmail_default">many redirects, ...etc.</div><div style="font-size:small" class="gmail_default"><br></div>A<span class="gmail_default" style="font-size:small"> brief tutorial can be found here (and elsewhere)<br></span></div><div class="gmail_quote"><span class="gmail_default" style="font-size:small"><br></span></div><div class="gmail_quote"><a href="https://www.geeksforgeeks.org/python-requests-tutorial/">https://www.geeksforgeeks.org/python-requests-tutorial/</a><div style="font-size:small" class="gmail_default"><br></div></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Khalid M. Baheyeldin<br><br></div></div>
</div>
</div>
</div>