首页 > 解决方案 > 如何在 bash 中以 Chrome 的方式对我的 curl URL 进行 URL 编码,在 Macos 上,这样我的 API 服务将不会返回 404

问题描述

我试图在 bash 中对我的 url 进行 urlencode。首先,我从需要传递给 API 服务的所有变量中组装 URL。然后我尝试使用这两种方法

# the unencoded string
echo "?token=$APITOKEN&calendar_id=$CALID&title=$TITLE&eventname=$ENAME&description=$DESC&location=$GU_LINK&organizer=$ORG&organizer_email=$ORGEMAIL&timezone=$TZ&reminder=$REM&start_date=$START&end_date=$END&all_day_event=$ALLDAY&rsvp_require=$RSVP"

# encoded string
URLC=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "?token=$APITOKEN&calendar_id=$CALID&title=$TITLE&eventname=$ENAME&description=$DESC&location=$GU_LINK&organizer=$ORG&organizer_email=$ORGEMAIL&timezone=$TZ&reminder=$REM&start_date=$START&end_date=$END&all_day_event=$ALLDAY&rsvp_require=$RSVP")
echo $URLC

# method two
FOO=$(printf %s "?token=$APITOKEN&calendar_id=$CALID&title=$TITLE&eventname=$ENAME&description=$DESC&location=$GU_LINK&organizer=$ORG&organizer_email=$ORGEMAIL&timezone=$TZ&reminder=$REM&start_date=$START&end_date=$END&all_day_event=$ALLDAY&rsvp_require=$RSVP" |jq -sRr @uri)
echo FOO $FOO

这是变量替换后未编码的基本字符串

?token=api....&calendar_id=156....title=Comedy and Q&A with Max Amini&eventname=Comedy and Q&A with Max Amini&description=Fun QA with Comedian Max Amini! Max talks comedy

这是我使用 curl -v 从上面的 perl 或 jq 编码中得到的:

GET /api/v1/oe/events/create/%3Ftoken%3Dapi15...%26calendar_id%3D156...%26title%3DComedy%20and%20Q%26A%20with%20Max%20Amini%26eventname%3DComedy%20and%20Q%26A%20with%20Max%20Amini%26description%3DFun%20QA%20with%20Comedian%20Max%20Amini!%20Max%20talks%20comedy%20and%20answers%20all%20your%20questions!%26location%3Dhttps%3A%2F%2Fsweetsummer.live%2Fvo-TTID%26organizer%3DSweet%20Summer%26organizer_email%3Dcontact%40sweetsummer.live%26timezone%3DPDT%26reminder%3D60%26start_date%3D9%2F2%2F2021%2015%3A00%3A00%26end_date%3D9%2F2%2F2021%2015%3A30%3A00%26all_day_event%3Dfalse%26rsvp_require%3Dfalse HTTP/1.1

> Host: www.NNNNNNN.com
> User-Agent: curl/7.64.1
> Accept: */*
> 
< HTTP/1.1 404 Not Found <-- Is this the filename it is trying for, or just a message about the protocol?
< Content-Type: text/html; charset=iso-8859-1
< Date: Sun, 29 Aug 2021 23:38:48 GMT
< Server: Apache
< Content-Length: 196
< Connection: keep-alive
< 

当我将未编码的 URL 粘贴到 Chrome URL 栏中时,它转换的字符串要少得多,并且服务会愉快地响应。

这是 Chrome URLencoded URL 的示例,Chrome 给了我这个编码:

https://www.NNNNNN.com/?token=api15613...&calendar_id=156...&title=Comedy%20and%20Q&A%20with%20Max%20Amini&eventname=Comedy%20and%20Q&A%20with%20Max%20Amini&description=Fun%20QA%20with%20Comedian%20Max%20Amini!%20Max%20talks%20comedy%20and%20answers%20all%20your%20questions!&location=https://sweetsummer.live/vo-TTID&organizer=Sweet%20Summer&organizer_email=contact@sweetsummer.live&timezone=PDT&reminder=60&start_date=9/2/2021%2015:00:00&end_date=9/2/2021%2015:30:00&all_day_event=false&rsvp_require=false

curl -v 使用上述方法,然后我的 JSON api 返回正常。是否出于某种原因将 HTTP 1.1 附加到我的字符串的问题?如果不是,那是什么?

谢谢!

标签: urlurlencode

解决方案


推荐阅读