SA-MP Forums Archive
HTTP to translate message - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: HTTP to translate message (/showthread.php?tid=423816)



HTTP to translate message - pasha97 - 19.03.2013

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?


Re: HTTP to translate message - Misiur - 19.03.2013

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


Re: HTTP to translate message - pasha97 - 19.03.2013

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) :/


Re: HTTP to translate message - Misiur - 19.03.2013

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");
}



Re: HTTP to translate message - pasha97 - 19.03.2013

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!


Re: HTTP to translate message - Misiur - 19.03.2013

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


Re: HTTP to translate message - pasha97 - 19.03.2013

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