1
私はそのようなコードを持っている:CURLの奇妙な行動(エラー400)作っhttpリクエスト
https://translate.yandex.net/api/v1.5/tr.json/translate?key=secret.key.here&lang=ru&text="Hello world. H"
:
const char url[] = "https://translate.yandex.net/api/v1.5/tr.json/translate";
const char key[] = "secret.key.here";
char buf[4096] = { 0 };
char input[1024] = "Hello world. H";
snprintf(buf, sizeof(buf), "%s?key=%s&lang=ru&text=\"%s\"",
url, key, input);
struct string response;
init_string(&response);
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, buf);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
printf("%s\n", response.data);
snprintf
実行後は、buf
は、このようなURLが含まれています応答後、response.data
は、以下を含む:
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
char input[1024] = "Hello world.";
(「H」なし)を再割り当てすると、httpリクエスト後に適切な応答が得られます。 {"code":200,"lang":"en-ru","text":["\"Здравствуй, мир!\"."]}
何が問題なのですか?
英語以外のテキストを送信しようとしましたか? WebサイトはHTTP応答コード400を使用して実際の英語を送信する必要があるか、翻訳できないことを伝えている可能性があります。だからそれはカール自体とは関係がありません。 –
@TJohnson 'echo -n" Hello world。T "| ./translate {"code":200、 "lang": "en-ru"、 "text":["\"Здравствуй、мир。 Т\ ""]}}。端末http://s32.postimg.org/lsber5asl/Screen_Shot_2016_05_01_at_20_30_51.pngからのスクリーンショット。しかし、ブラウザから同じ要求をすると、コード200が得られます。 – 0x1337