【问题标题】:Curl request different from browser与浏览器不同的 curl 请求
【发布时间】:2016-01-11 11:32:29
【问题描述】:

我正在尝试从 ESA 访问 API。详情here 使用 curl 时,

curl -svgu <user>:<password> "https://scihub.esa.int/dhus/odata/v1/Products?$filter=year(IngestionDate) eq 2015 and month(IngestionDate) eq 10 and day(IngestionDate) eq 10"

回复者的日期错误(2014 年)

在 chrome 上使用相同的链接会产生预期的结果(日期已过的文件)

https://scihub.esa.int/dhus/odata/v1/Products?$filter=year(IngestionDate) eq 2015 and month(IngestionDate) eq 10 and day(IngestionDate) eq 10

我尝试删除空格并使用

`"https://scihub.esa.int/dhus/odata/v1/Products?$filter=year(IngestionDate)%20eq%202015%20and%20month(IngestionDate)%20eq%2010%20and%20day(IngestionDate)%20eq%2010"`

卷曲。

有什么建议或想法吗?您可以使用这些链接进行尝试。

编辑: 根据Pafjo的建议, curl -G -gu : "https://scihub.esa.int/dhus/odata/v1/Products" --data-urlencode '$filter=year(IngestionDate) eq '"$YEAR and month(IngestionDate) eq $MONTH and day(IngestionDate) eq $DAY" 这就是答案

【问题讨论】:

    标签: api http curl


    【解决方案1】:
    1. Curl 参数可能容易被您使用的 shell 解释。您可能需要转义正则表达式字符,在本例中为 ( 和 )。
    2. URL 中不允许有空格。您应该将它们替换为 %20。

    【讨论】:

    • 你能不能帮我制作 1 号?
    • 我尝试使用你的建议,但我遇到了同样的问题 "Products?$filter=year(\IngestionDate)\%20eq%202015"
    • 括号前的斜线:\(IngestionDate\)
    • 我之前、之后、之前和之后都试过了。我的评论前后都有,但没有显示(不知道为什么)
    【解决方案2】:

    Curl 不对您的 url 进行 url 编码,因此您需要告诉它应该对哪部分进行 url 编码。

    试试这个,看看你的回答是否正确:

    curl -G -svgu  <user>:<password> "https://scihub.esa.int/dhus/odata/v1/Products" --data-urlencode '$filter=year(IngestionDate) eq 2015 and month(IngestionDate) eq 10 and day(IngestionDate) eq 10'
    

    第二个 curl 命令失败的原因是双引号中有 $filter。因此,当您执行请求时,shell 会将您的变量替换为值。这可以通过使用单引号或转义美元符号来解决。

    【讨论】:

    • 谢谢!那行得通!最后一个问题,我正在使用带有变量的日期,我应该如何将其传递给字符串?我正在尝试这样 --data-urlencode '$filter=year(IngestionDate) eq '"$YEAR"' and month(IngestionDate) eq '"$MONTH"' and day(IngestionDate) eq '"$DAY"
    • 好的。尝试使用双引号但转义第一个美元符号: curl -G -svgu user:password "scihub.esa.int/dhus/odata/v1/Products" --data-urlencode "\$filter=year(IngestionDate) eq $year and month(IngestionDate) eq 10 和day(IngestionDate) eq 10"
    • 不起作用。我正在考虑构建一个字符串并将其附加到 '$filter=year(IngestionDate)'
    • 设法用 --data-urlencode '$filter=year(IngestionDate) eq '"$YEAR and month(IngestionDate) eq $MONTH and day(IngestionDate) eq $DAY"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2023-03-10
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多