[英]How to send a simple HTML file from a bash script with the curl command using SendGrid's SMTP API?
[英]Send unserialized & unescaped HTML file data to an API with a bash script
我想創建一個 bash 腳本,該腳本采用 HTML 文件並將其發送到多個 API。
我有一個test.html
文件,其中包含未序列化的 HTML 數據,如下所示:
<h2 id="overview">Overview</h2>
<p>Have the source of truth in your own space at <strong>somewhere</strong></p>
<pre>
<code class="lang-javascript">function go() {
console.log('code blocks can be a pain');
}
go();
</code>
</pre>
我需要以某種方式將文件的內容發送到 API,如下所示:
curl --location --request POST 'https://devo.to/api/articles' \
--header 'api-key: askldjfalefjw02ijef02eifj20' \
--header 'Content-Type: application/json' \
--data-raw '{
"article": {
"title": "Blog Article",
"body_markdown": "@test.html",
}
}'
到目前為止,我能想到的唯一方法是序列化/轉義 HTML 文件,將其作為字符串讀入變量(如$TEST_HTML=$(cat serialized_test.html
),然后將其傳遞給"body_markdown"
。
是否可以在 bash 腳本中一步序列化/轉義 HTML 或者是否有更好的方法?
我會使用jq
來構建 JSON 參數,並讓它正確處理包含的 HTML 文件中的 escaping 引號、換行符等:
curl --location --request POST 'https://devo.to/api/articles' \
--header 'api-key: askldjfalefjw02ijef02eifj20' \
--header 'Content-Type: application/json' \
--data-raw "$(jq -n --arg html "$(< test.html)" '{article:{title:"Blog Article",body_markdown:$html}}')"
jq
調用將test.html
的內容放入字符串變量$html
中,計算結果為:
{
"article": {
"title": "Blog Article",
"body_markdown": "<h2 id=\"overview\">Overview</h2>\n<p>Have the source of truth in your own space at <strong>somewhere</strong></p>\n<pre>\n<code class=\"lang-javascript\">function go() {\n console.log('code blocks can be a pain');\n}\ngo();\n</code>\n</pre>"
}
}
$(< filename)
是一個bash
替換,計算給定文件的內容。 在bash
中,它比$(cat filename)
更受歡迎,因為它不涉及運行另一個進程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.