This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/08/22)


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Stewart Anubis
Junior Member
Join Date: Jul 2020
Old 01-25-2022 , 15:03   Re: REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/08/22)
Reply With Quote #39

Does anyone know how I can convert this "[[["Hola","hello",null,null,10]],null,"en",null,null,null,null,[]]" to "Hola" using #include <ripext> ?

Code:
Handle CreateRequest(char[] input, char[] target)
{
    
    Handle request = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, "http://translate.googleapis.com/translate_a/single");
    SteamWorks_SetHTTPRequestGetOrPostParameter(request, "client", "gtx");
    SteamWorks_SetHTTPRequestGetOrPostParameter(request, "dt", "t");    
    SteamWorks_SetHTTPRequestGetOrPostParameter(request, "sl", "auto");//from en default, so you might wanna add this param too to modify.
    SteamWorks_SetHTTPRequestGetOrPostParameter(request, "tl", target);//to desired.. target language
    SteamWorks_SetHTTPRequestGetOrPostParameter(request, "q", input);//input text

    //final url would be something like this  https://translate.googleapis.com/tra...&tl=es&q=hello
    //response example [[["Hola","hello",null,null,10]],null,"en",null,null,null,null,[]]  so we need to parse json on Callback_OnHTTPResponse >  JSON.parse(response)[0][0][0] = Hola
    
    Handle datapack = CreateDataPack();
    WritePackString(datapack, target);
    WritePackString(datapack, input);

    
    SteamWorks_SetHTTPRequestContextValue(request, datapack);
    SteamWorks_SetHTTPCallbacks(request, Callback_OnHTTPResponse);
    return request;
}

public int Callback_OnHTTPResponse(Handle request, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, Handle datapack)
{
	if (!bRequestSuccessful || eStatusCode != k_EHTTPStatusCode200OK)
	{	  
		CloseHandle(datapack);		
		return;
	}

	int iBufferSize;
	SteamWorks_GetHTTPResponseBodySize(request, iBufferSize);

	char[] result = new char[iBufferSize];
	SteamWorks_GetHTTPResponseBodyData(request, result, iBufferSize);
	delete request;
	
	char target[3], input[255];
	ResetPack(datapack);
	ReadPackString(datapack, target, 3);
	ReadPackString(datapack, input, 255);
	CloseHandle(datapack);

	PrintToServer(result); // = [[["Hola","hello",null,null,10]],null,"en",null,null,null,null,[]]
}

Last edited by Stewart Anubis; 01-25-2022 at 15:09.
Stewart Anubis is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 14:14.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode