我想从命令行执行一个相当复杂的具有多部分/混合边界的 HTTP 请求。
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:[email protected] /cdn-cgi/l/email-protection>
Content-Transfer-Encoding: binary
POST /drive/v2/files/fileId/permissions
Content-Type: application/json
Content-Length: 71
{
"role": "reader",
"type": "user",
"value": "[email protected] /cdn-cgi/l/email-protection"
}
--batch_0123456789
Content-Type: application/http
Content-ID: <item2:[email protected] /cdn-cgi/l/email-protection>
Content-Transfer-Encoding: binary
POST /drive/v2/files/fileId/permissions
Content-Type: application/json
Content-Length: 71
{
"role": "reader",
"type": "user",
"value": "[email protected] /cdn-cgi/l/email-protection"
}
--batch_0123456789--
理想情况下,我想将此请求放入文件中,然后简单地调用curl 来执行该HTTP 请求。
curl myrequest.txt
有没有简单直接的方法可以做到这一点?我知道有些客户端库有其惯用的处理方式,但我有兴趣了解是否有办法从命令行执行此操作。
您可以使用--config http://curl.haxx.se/docs/manpage.html#-K选项(请参阅“配置文件”部分manual http://curl.haxx.se/docs/manual.html更多细节):
curl --config myrequest.txt
我认为没有一种干净的方法可以在配置文件中嵌入多行 POST 正文。您可以将每个换行符替换为\r\n
(多部分请求需要 CRLF 换行符):
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:[email protected] /cdn-cgi/l/email-protection>\r\nContent-Transfer-Encoding: binary\r\n\r\n..."
但这不太容易阅读。
或者,您可以将 POST 正文放在单独的文件中。例如:
我的请求.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:[email protected] /cdn-cgi/l/email-protection>
Content-Transfer-Encoding: binary
POST /drive/v2/files/fileId/permissions
Content-Type: application/json
Content-Length: 71
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)