有没有一种方法可以传递文件内容进行卷曲? [英] Is there a way to pass the content of a file to curl?

查看:92
本文介绍了有没有一种方法可以传递文件内容进行卷曲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从命令行执行一个具有多部分/混合边界的相当复杂的HTTP请求。

I would like to execute a fairly complex HTTP request with multipart/mixed boundaries from the command line.

POST /batch HTTP/1.1
Host: www.googleapis.com
Content-length: 592
Content-type: multipart/mixed; boundary=batch_0123456789
Authorization: Bearer authorization_token

--batch_0123456789
Content-Type: application/http
Content-ID: <item1:user@example.com>
Content-Transfer-Encoding: binary


POST /drive/v2/files/fileId/permissions
Content-Type: application/json
Content-Length: 71


{
  "role": "reader",
  "type": "user",
  "value": "user@example.com"
} 


--batch_0123456789
Content-Type: application/http
Content-ID: <item2:user@example.com>
Content-Transfer-Encoding: binary


POST /drive/v2/files/fileId/permissions
Content-Type: application/json
Content-Length: 71


{
  "role": "reader",
  "type": "user",
  "value": "user@example.com"
}


--batch_0123456789--

理想情况下,我想将此请求放入文件中,然后简单地调用curl执行该HTTP请求。

Ideally I would like to put this request into a file and then simply call curl to execute that HTTP request.

curl myrequest.txt

是否有任何简单易行的方法?我知道有些客户端库有其惯用的方式来处理此问题,但我很想知道是否可以从命令行中执行此操作。

Is there any easy straightforward way of doing this? I understand that there are client libraries that have their idiomatic ways of handling this, but I am interested to find out if there is a way to do this from the command line.

推荐答案

您可以使用 -config 选项(请参见手册(了解更多详细信息)):

You can use the --config option (see the "CONFIG FILE" section of the manual for more details):

curl --config myrequest.txt

我认为没有一种干净的方法可以在配置文件中嵌入多行POST正文。您可以使用 \r\n 替换每个换行符(对于多部分请求,需要CRLF换行符):

I don't think there's a clean way to embed a multiline POST body within the config file. You could replace each newline character with \r\n (CRLF newlines are required for multipart requests):

url = "http://www.googleapis.com/batch"
header = "Content-length: 592"
header = "Content-type: multipart/mixed; boundary=batch_0123456789"
header = "Authorization: Bearer authorization_token"
data-binary = "--batch_0123456789\r\nContent-Type: application/http\r\nContent-ID: <item1:user@example.com>\r\nContent-Transfer-Encoding: binary\r\n\r\n..."

但这不是很容易阅读。

或者,您可以放置​​POST正文在一个单独的文件中。例如:

Alternatively, you could put the POST body in a separate file. For example:

myrequest.txt

url = "http://www.googleapis.com/batch"
header = "Content-length: 592"
header = "Content-type: multipart/mixed; boundary=batch_0123456789"
header = "Authorization: Bearer authorization_token"
data-binary = "@myrequestbody.txt"

myrequestbody.txt

--batch_0123456789
Content-Type: application/http
Content-ID: <item1:user@example.com>
Content-Transfer-Encoding: binary


POST /drive/v2/files/fileId/permissions
Content-Type: application/json
Content-Length: 71

...

这篇关于有没有一种方法可以传递文件内容进行卷曲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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