HTTP to translate message
#1

I am trying to create function to send players translated messages. This is the code:

pawn Код:
SendTranslatedMessage(playerid,message[])
{
    new buf[1024];
    format(buf,sizeof(buf),"translate.yandex.net/tr.json/translate?lang=en-ru&text=%s",message);
     HTTP(playerid, HTTP_GET, buf, "", "MyHttpResponse");
}
forward MyHttpResponse(index, response_code, data[]);
public MyHttpResponse(index, response_code, data[])
{

    if(response_code == 200) SendClientMessage(index,0xFFFFFFFF,data);

}
It works so: When the function is called, it sends an http request and gets the translation. For example, if I use:

Код:
SendTranslatedMesage(playerid,"Hello, world!")
it will form a link:

Код:
translate.yandex.net/tr.json/translate?lang=en-ru&text=Hello, World!
and send an HTTP request. The data must be:

Код:
"Здравствуй, Мир!"
So player must see it. But I get a response code 6, which means HTTP_ERROR_MALFORMED_RESPONSE. What's the problem?
Reply
#2

http://dracoblue.net/dev/urlencode-in-pawn/141/ Your url is incorrect
Reply
#3

Quote:
Originally Posted by Misiur
Посмотреть сообщение
Now i changed it to this:
pawn Код:
SendTranslatedMessage(playerid,message[])
{
    new buf[1024];
    format(buf,sizeof(buf),"translate.yandex.net/tr.json/translate?lang=en-ru&text=%s",message);
    printf("Request goes: %s",buf);
     HTTP(playerid, HTTP_GET, urlencode(buf), "", "MyHttpResponse");
}
And response code is now 1(HTTP_ERROR_BAD_HOST) :/
Reply
#4

In "request goes" you have some weird string with a lot of %'s? You only have to urlencode parameters
pawn Код:
SendTranslatedMessage(playerid,message[])
{
    new buf[1024];
    format(buf,sizeof(buf),"translate.yandex.net/tr.json/translate?lang=en-ru&text=%s",urlencode(message));
    printf("Request goes: %s",buf);
     HTTP(playerid, HTTP_GET, buf, "", "MyHttpResponse");
}
Reply
#5

Quote:
Originally Posted by Misiur
Посмотреть сообщение
In "request goes" you have some weird string with a lot of %'s? You only have to urlencode parameters
pawn Код:
SendTranslatedMessage(playerid,message[])
{
    new buf[1024];
    format(buf,sizeof(buf),"translate.yandex.net/tr.json/translate?lang=en-ru&text=%s",urlencode(message));
    printf("Request goes: %s",buf);
     HTTP(playerid, HTTP_GET, buf, "", "MyHttpResponse");
}
Done like you said, again request code 6. And this is what I see in log when trying to translate "Welcome, Administrator!"
Request goes: translate.yandex.net/tr.json/translate?lang=en-ru&text=Welcome%2C%20Administrator!
Reply
#6

The path is correct. However there is a little problem - the site you are requesting is returning "application/json" document type, not "text/html" or something. I'll dig around and check if HTTP can handle this
Reply
#7

Quote:
Originally Posted by Misiur
Посмотреть сообщение
The path is correct. However there is a little problem - the site you are requesting is returning "application/json" document type, not "text/html" or something. I'll dig around and check if HTTP can handle this
ok. Thank you
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)