I got a strange problem today, a URL is supposed to return something in it’s body, but when use curl to get that url, it outputs nothing.

curl 'http://localhost:9527/static/a.js'

At first, I think it might be server problems, but when using wireshark to see the traffic, there is nothing wrong, http header followed by \r\n then http response body. I have also noticed that this happens when the response body is short. Testing with browser or python-requests is fine.

So it’s not the server error, could it be curl’s problem? Let’s strace it.

>> strace -vv curl 'http://localhost:9527/static/a.js'
......
recvfrom(5, "HTTP/1.1 200 OK\r\nServer: YNM3K-9"..., 102400, 0, NULL, NULL) = 443
newfstatat(1, "", {st_dev=makedev(0, 0x1d), st_ino=6, st_mode=S_IFCHR|0620, st_nlink=1, st_uid=1000, st_gid=5, st_blksize=1024, st_blocks=0, st_rdev=makedev(0x88, 0x3), st_atime=1703671640 /* 2023-12-27T18:07:20.893334033+0800 */, st_atime_nsec=893334033, st_mtime=1703671640 /* 2023-12-27T18:07:20.893334033+0800 */, st_mtime_nsec=893334033, st_ctime=1703662393 /* 2023-12-27T15:33:13.893334033+0800 */, st_ctime_nsec=893334033}, AT_EMPTY_PATH) = 0
brk(0x560907e02000)                     = 0x560907e02000
rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[PIPE], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f68b677f710}, NULL, 8) = 0
rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[PIPE], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f68b677f710}, NULL, 8) = 0
write(1, "a.js", 4a.js)                     = 4
rt_sigaction(SIGPIPE, NULL, {sa_handler=SIG_IGN, sa_mask=[PIPE], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f68b677f710}, 8) = 0
......

From strace output, I can see it write a.js to fd 1, which is stdout, so it get the response body, and write to stdout, maybe it’s the terminal?

I use zsh as default shell, so I tried same command in bash, and it worked, what happened?

After google, someone mat the same problem, like https://github.com/NixOS/nixpkgs/issues/30121 and https://stackoverflow.com/questions/58451515/curl-on-macos-catalina-not-displaying-response-body

Conclution: it has something to do with zsh prompt

Fix:

# add 'setopt prompt_sp' to  ~/.zshrc
promptinit
prompt suse
setopt prompt_sp