Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically add newline at end of curl response body

Tags:

curl

People also ask

How do you use the curl command to respond?

Append a line "http_code:200" at the end, and then grep for the keyword "http_code:" and extract the response code. In this case, you can still use the non-silent mode / verbose mode to get more information about the request such as the curl response body.

Does curl write to stdout?

When asking curl to get a URL it'll send the output to stdout by default. You can of course easily change this behavior with options or just using your shell's redirect feature, but without any option it'll spew it out to stdout.

What is curl command line?

cURL, which stands for client URL, is a command line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.


From the man file:

To better allow script programmers to get to know about the progress of curl, the -w/--write-out option was introduced. Using this, you can specify what information from the previous transfer you want to extract.

To display the amount of bytes downloaded together with some text and an ending newline:

curl -w 'We downloaded %{size_download} bytes\n' www.download.com

So try adding the following to your ~/.curlrc file:

-w "\n"

Use this:

curl jsonip.com; echo 

If you need grouping to feed a pipe :

{ curl jsonip.com; echo; } | tee new_file_with_newline

OUTPUT

{"ip":"x.x.x.x","about":"/about"}

This is that simple ;)

(and not limited to curl command but all commands that not finish with a newline)


For more info as well as a clean new line after curl

~/.curlrc

-w "\nstatus=%{http_code} %{redirect_url} size=%{size_download} time=%{time_total} content-type=\"%{content_type}\"\n"

(More options are available here)

redirect_url will be blank if the request doesn't get redirected or you use -L to follow the redirect.

Example output:

~ ➤  curl https://www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://www.google.co.uk/?gfe_rd=cr&amp;ei=FW">here</A>.
</BODY></HTML>

status=302 https://www.google.co.uk/?gfe_rd=cr&ei=FW size=262 time=0.044209 content-type="text/html; charset=UTF-8"
~ ➤  

Edit, to make things more readable you can add ANSI colours to the -w line, it's not that easy to write directly, but this script can generate a ~/.curlrc file with colours.

#!/usr/bin/env python3
from pathlib import Path
import click
chunks = [
    ('status=', 'blue'),
    ('%{http_code} ', 'green'),
    ('%{redirect_url} ', 'green'),
    ('size=', 'blue'),
    ('%{size_download} ', 'green'),
    ('time=', 'blue'),
    ('%{time_total} ', 'green'),
    ('content-type=', 'blue'),
    ('\\"%{content_type}\\"', 'green'),
]
content = '-w "\\n'
for chunk, colour in chunks:
    content += click.style(chunk, fg=colour)
content += '\\n"\n'

path = (Path.home() / '.curlrc').resolve()
print('writing:\n{}to: {}'.format(content, path))
path.write_text(content)

The general solution for bash is to add a newline symbol into the command prompt:

See related question (How to have a newline before bash prompt? ) and corresponding answer

This solution covers each command, not only curl.

echo $PS1 # To get your current PS1 env variable's value aka '_current_PS1_'
PS1='\n_current_PS1_'

The only side-effect is that you get command prompt after each 2nd line.