为什么cURL返回错误“(23)书写主体失败"? [英] Why does cURL return error "(23) Failed writing body"?

查看:474
本文介绍了为什么cURL返回错误“(23)书写主体失败"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以作为单个工具正常使用

It works ok as a single tool:

curl "someURL"
curl -o - "someURL"

但它不能在管道中工作

curl "someURL" | tr -d '\n'
curl -o - "someURL" | tr -d '\n'

它返回:

(23) Failed writing body

用管道传输cURL输出有什么问题?如何缓冲整个cURL输出然后处理它?<​​/p>

What is the problem with piping the cURL output? How to buffer the whole cURL output and then handle it?

推荐答案

当管道程序(例如grep)在上一个程序写完整个页面之前关闭读取管道时,就会发生这种情况.

This happens when a piped program (e.g. grep) closes the read pipe before the previous program is finished writing the whole page.

curl "url" | grep -qs foo中,只要grep拥有所需的内容,它将关闭curl的读取流. cURL不会这样,并会发出书写体失败"错误.

In curl "url" | grep -qs foo, as soon as grep has what it wants it will close the read stream from curl. cURL doesn't expect this and emits the "Failed writing body" error.

一种解决方法是将流通过中间程序传递给中间程序,该中间程序在将其馈送到下一个程序之前始终会读取整个页面.

A workaround is to pipe the stream through an intermediary program that always reads the whole page before feeding it to the next program.

例如

curl "url" | tac | tac | grep -qs foo

tac是一个简单的Unix程序,它读取整个输入页并反转行顺序(因此我们将其运行两次).由于必须读取整个输入才能找到最后一行,因此在cURL完成之前,它不会向grep输出任何内容. Grep拥有所需的内容时仍会关闭读取流,但只会影响tac,不会发出错误.

tac is a simple Unix program that reads the entire input page and reverses the line order (hence we run it twice). Because it has to read the whole input to find the last line, it will not output anything to grep until cURL is finished. Grep will still close the read stream when it has what it's looking for, but it will only affect tac, which doesn't emit an error.

这篇关于为什么cURL返回错误“(23)书写主体失败"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆