Raised This Month: $120 Target: $400
 30% 

REST in Pawn - Communicate with JSON REST APIs


Post New Thread Reply   
 
Thread Tools Display Modes
slekenda
Junior Member
Join Date: Aug 2017
Old 11-07-2017 , 10:39   Re: REST in Pawn - Communicate with JSON REST APIs
Reply With Quote #41

Hello!

I'm having a problem with my Post command. My server gets the post JSON data, but the plugin won't execute the function after. I'm calling DoSomething() function in OnPluginStart(). My code:
PHP Code:
public void DoSomething(){
    
PrintToServer("We are in DoSomething");
    
httpClient = new HTTPClient("http://ip.to.my.server");

    
JSONObject port = new JSONObject();
    
port.SetInt("port"iPort);

    
httpClient.Post("api/get_postfromcsgoserver"portOnPortResponse);

    
PrintToServer("We are in DoSomething2");
    
delete port;

}

public 
void OnPortResponse(HTTPResponse responseany value){
    
PrintToServer("We are in OnPortResponse");
    if(
response.Status != HTTPStatus_Created) {
        
PrintToServer("!= HTTPStatus_Created");
        return;
    }
    if (
response.Data == null){
        
PrintToServer("response.data == null");
        return;
    }

The latter is just a part of the function. I don't get any compilation errors. It won't even print the "We are in OnPortResponse" to the server.

Does connection have to be https?

I get this from my server that the plugin tries to Post so it's working
[07/Nov/2017 14:28:09] "POST /api/get_postfromcsgoserver/ HTTP/1.1" 200 98

Thank you!

Last edited by slekenda; 11-07-2017 at 10:44.
slekenda is offline
ReymonARG
Junior Member
Join Date: Jun 2017
Old 11-07-2017 , 20:24   Re: REST in Pawn - Communicate with JSON REST APIs
Reply With Quote #42

You need to turn off the invernation of the server, or enter to the server.

Quote:
Originally Posted by slekenda View Post
Hello!

I'm having a problem with my Post command. My server gets the post JSON data, but the plugin won't execute the function after. I'm calling DoSomething() function in OnPluginStart(). My code:
PHP Code:
public void DoSomething(){
    
PrintToServer("We are in DoSomething");
    
httpClient = new HTTPClient("http://ip.to.my.server");

    
JSONObject port = new JSONObject();
    
port.SetInt("port"iPort);

    
httpClient.Post("api/get_postfromcsgoserver"portOnPortResponse);

    
PrintToServer("We are in DoSomething2");
    
delete port;

}

public 
void OnPortResponse(HTTPResponse responseany value){
    
PrintToServer("We are in OnPortResponse");
    if(
response.Status != HTTPStatus_Created) {
        
PrintToServer("!= HTTPStatus_Created");
        return;
    }
    if (
response.Data == null){
        
PrintToServer("response.data == null");
        return;
    }

The latter is just a part of the function. I don't get any compilation errors. It won't even print the "We are in OnPortResponse" to the server.

Does connection have to be https?

I get this from my server that the plugin tries to Post so it's working
[07/Nov/2017 14:28:09] "POST /api/get_postfromcsgoserver/ HTTP/1.1" 200 98

Thank you!
ReymonARG is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 11-08-2017 , 01:22   Re: REST in Pawn - Communicate with JSON REST APIs
Reply With Quote #43

Quote:
Originally Posted by ReymonARG View Post
You need to turn off the invernation
I think you mean Hibernation.
__________________
Mitchell is offline
slekenda
Junior Member
Join Date: Aug 2017
Old 11-08-2017 , 04:12   Re: REST in Pawn - Communicate with JSON REST APIs
Reply With Quote #44

Quote:
Originally Posted by ReymonARG View Post
You need to turn off the invernation of the server, or enter to the server.
Thank you! I have been wondering this now for a while. This was the problem, I never joined the server. And it's hibernation
slekenda is offline
ReymonARG
Junior Member
Join Date: Jun 2017
Old 11-08-2017 , 12:01   Re: REST in Pawn - Communicate with JSON REST APIs
Reply With Quote #45

I Hope that plugin author change to server think and not the game frame
ReymonARG is offline
CrazyHackGUT
AlliedModders Donor
Join Date: Feb 2016
Location: Izhevsk, Russia
Old 02-09-2018 , 16:46   Re: REST in Pawn - Communicate with JSON REST APIs
Reply With Quote #46

So, i forked this extension 31 oct. 2017, and made small changes.
  • Fixed segfault
  • Added timeout connection and waiting response, "Follow location" settings in HTTPClient methodmap.
  • IChangeableForward removed, now extension uses IPluginFunction.
  • Callback modified: added ability for receive error string.
    Code:
    public void OnRequestFinished(HTTPResponse hResponse, any iData, const char[] szError) {
        if (szError[0] != 0) {
            LogError("Error received when processing request: %s", szError);
            return;
        }
    
        // handle response...
    }
    But you can still use this old function prototype, without receiving error message:
    Code:
    public void OnRequestFinished(HTTPResponse hResponse, any iData) {
        // handle response...
    }
    But in this case, you should add check:
    Code:
    if (hResponse != null)

You can see source code here. Download binaries here.
__________________
My english is very bad. I am live in Russia. Learning english language - very hard task for me...
CrazyHackGUT is offline
Send a message via ICQ to CrazyHackGUT Send a message via Skype™ to CrazyHackGUT
muukis
Veteran Member
Join Date: Apr 2009
Old 07-08-2018 , 13:46   Re: REST in Pawn - Communicate with JSON REST APIs
Reply With Quote #47

Sorry if this has been asked before, but...
Is there a way to allow the use of self signed SSL certificates? I am getting a BADCERT_CN_MISMATCH error when using a self signed HTTPS URI rest api.

Code:
L 07/08/2018 - 19:32:20: [RIPEXT] HTTP request failed: Cert verify failed: BADCERT_CN_MISMATCH
__________________
Monster Hunter

Though certainly not superhuman, the man's prowess inspires an excess of whispered rumors. But those rumors remain in the realm of speculation.
muukis is offline
Santi.
Member
Join Date: Oct 2010
Location: Cordoba(Argentina)
Old 07-11-2018 , 20:56   Re: REST in Pawn - Communicate with JSON REST APIs
Reply With Quote #48

Excellent job!
Santi. is offline
muukis
Veteran Member
Join Date: Apr 2009
Old 07-13-2018 , 05:42   Re: REST in Pawn - Communicate with JSON REST APIs
Reply With Quote #49

I managed to get the self signed certificate to work - Thanks for helping me out at GitHub.

However, I have another problem... this does not work:
Code:
#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
	PrintToServer("Testing JSONObject...");
	JSONObject obj = new JSONObject();
	obj.SetString("test", "");
	decl String:test[10];
	obj.GetString("test", test, sizeof(test));
	delete obj;
	PrintToServer("test: %s", test);
	PrintToServer("Finished testing JSONObject");
}
What the server responds is this:
Code:
sm plugins load test
Testing JSONObject...
L 07/13/2018 - 11:35:58: [SM] Native "JSONObject.GetString" reported: Could not retrieve value for key 'test'
L 07/13/2018 - 11:35:58: [SM] Displaying call stack trace for plugin "test.smx":
L 07/13/2018 - 11:35:58: [SM]   [0]  Line 10, test.sp::OnPluginStart()
[SM] Loaded plugin test.smx successfully.
I am trying to send player name to a REST API. It seems some player names fail to work with the JSONObject. Not always though, and that is what I don't quite understand. I am thinking base64 encoding the names, but I was hoping there was a better solution for this.

Here's another example:
Code:
#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
	PrintToServer("Testing JSONObject...");
	JSONObject obj = new JSONObject();
	obj.SetString("test1", "We start here...");
	obj.SetString("test2", "");
	obj.SetString("test3", ".. and here we are");
	decl String:json[64];
	obj.ToString(json, sizeof(json));
	delete obj;
	PrintToServer("json: %s", json);
	PrintToServer("Finished testing JSONObject");
}
What the server responds is this:
Code:
sm plugins load test
Testing JSONObject...
json: {"test1": "We start here...", "test3": ".. and here we are"}
Finished testing JSONObject
[SM] Loaded plugin test.smx successfully.
__________________
Monster Hunter

Though certainly not superhuman, the man's prowess inspires an excess of whispered rumors. But those rumors remain in the realm of speculation.

Last edited by muukis; 07-13-2018 at 06:25.
muukis is offline
SztangaBiceps
Junior Member
Join Date: Nov 2015
Old 08-15-2018 , 00:19   Re: REST in Pawn - Communicate with JSON REST APIs
Reply With Quote #50

Doesn't work
I cannot take value from page
SztangaBiceps is offline
Reply


Thread Tools
Display Modes

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 10:15.


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