如何使用curl发送换行符?

问题描述 投票:0回答:2

我正在尝试为其中一个目录中的每个文件创建融合页面。我正在使用下面的curl命令来实现它

$ cat test.txt
foo
bar

curl -i -X POST -H "Content-Type:application/json" -u username:password  -d  '{ "ancestors": [ { "id": "65601" } ], "body": { "storage": { "value": "'"$(cat test.txt)"'", "representation": "storage" } }, "space": { "key": "TEST" }, "status": "current", "title": "Page1", "type": "page" }' 'http://localhost:8090/rest/api/content';

这是在调试模式下的外观

curl -i -X POST -H "Content-Type:application/json" -u username:password  -d  '{ "ancestors": [ { "id": "65601" } ], "body": { "storage": { "value": "foo
bar", "representation": "storage" } }, "space": { "key": "TEST" }, "status": "current", "title": "Page1", "type": "page" }' 'http://localhost:8090/rest/api/content';

我什至使用此选项--data-binary进行了测试,都无法正常工作。

我希望文件(test.txt)的内容按原样反映在融合页面中,即它应按原样保留新行。

即使按如下所示放置\ n也没有用

"storage": { "value": "foo\nbar" }

任何建议如何使用curl实现此功能?

curl confluence
2个回答
0
投票

尝试--data-binary,但它仍然给出错误。我在这里缺少什么?

curl -i -X POST -H "Content-Type:application/json" -u username:password  --data-binary  '{ "ancestors": [ { "id": "65601" } ], "body": { "storage": { "value": "'"$(cat test.txt)"'", "representation": "storage" } }, "space": { "key": "TEST" }, "status": "current", "title": "page44huhhj3438", "type": "page" }' 'http://localhost:8090/rest/api/content';
++ cat test.txt
+ curl -i -X POST -H Content-Type:application/json -u username:password --data-binary '{ "ancestors": [ { "id": "65601" } ], "body": { "storage": { "value": "dfdhsadhsa
asdfj
asdfkja
adskjfb
adsjgkab", "representation": "storage" } }, "space": { "key": "TEST" }, "status": "current", "title": "page44huhhj3438", "type": "page" }' http://localhost:8090/rest/api/content
HTTP/1.1 500 
X-ASEN: SEN-L14404972
Set-Cookie: JSESSIONID=FD9034D646080D9A2036BDB2ED793804; Path=/; HttpOnly
X-Seraph-LoginReason: OK
X-AUSERNAME: admin
Cache-Control: no-cache, must-revalidate
Expires: Thu, 01 Jan 1970 00:00:00 GMT
X-Content-Type-Options: nosniff
Content-Type: application/json
Transfer-Encoding: chunked
Date: Thu, 17 Oct 2019 10:19:56 GMT
Connection: close

{"statusCode":500,"message":"","reason":"Internal Server Error"}

0
投票

--data-binary 保留并传递所有空格字符,包括换行符,而普通的-d/--data选项则不会。它不会单独引入它们,因此您需要确保数据包括它们。

也请避免将-X POST-d一起使用,因为充其量是不必要的,最坏的情况下会引起问题。

验证

[如果您想验证POST是否完全符合您的要求,我建议您在命令行中添加--trace-ascii dump.txt,然后在事后检查dump.txt文件并验证正文是否正确发送您想要的方式。

© www.soinside.com 2019 - 2024. All rights reserved.