通过curl测试谷歌翻译API

时间:2016-04-17 12:58:32

标签: google-translate

我正在通过curl测试Google Translate API,并且它一直在询问参数q,而它已经存在于URL中(q = Hello%20World)。为什么Google Translate API会返回此错误?

curl https://www.googleapis.com/language/translate/v2?key=& source = en& target = de& q = Hello%20World

[1] 16848
[2] 16849
[3] 16850
[2]-  Done                    source=en
[3]+  Done                    target=de
foo:~ foo$ {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Required parameter: q",
    "locationType": "parameter",
    "location": "q"
   },
   {
    "domain": "global",
    "reason": "required",
    "message": "Required parameter: target",
    "locationType": "parameter",
    "location": "target"
   }
  ],
  "code": 400,
  "message": "Required parameter: q"
 }
}

帮助?

1 个答案:

答案 0 :(得分:3)

您必须用双引号括住整个网址,例如:

curl "https://www.googleapis.com/language/translate/v2?key=&source=en&target=de&q=Hello%20World"

否则,shell会在实际执行&之前解释这些curl个字符;这就是为什么你在开始时看到所有这些线条的原因:

[1] 16848
[2] 16849
[3] 16850
[2]-  Done                    source=en
[3]+  Done                    target=de