AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Extensions (https://forums.alliedmods.net/forumdisplay.php?f=134)
-   -   REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/08/22) (https://forums.alliedmods.net/showthread.php?t=298024)

DJ Tsunami 05-30-2017 05:11

REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/08/22)
 
This extension exposes a high performance HTTP client for JSON REST APIs. It supports HTTP/2, HTTPS and gzip, and provides methodmaps for JSON objects and arrays.

Requires SourceMod 1.10 or later.

Note that requests are not processed during hibernation. You can set sv_hibernate_when_empty to 0 to disable hibernation.

Note that HTTP/2 is only attempted over HTTPS, in line with most web clients.


Installation
  1. Click Download.
  2. Download and extract the file for your server's operating system.
  3. Upload the files in the addons/sourcemod directory to your server.

Source Code
HTTP natives
JSON natives

https://img.shields.io/github/workfl...xt/CI/main.svg https://img.shields.io/github/releas.../sm-ripext.svg https://img.shields.io/github/downlo...pext/total.svg

DJ Tsunami 05-30-2017 05:11

REST in Pawn Code Examples
 
Code Examples

JSON

Create a JSON object

PHP Code:

JSONObject someObject = new JSONObject();

someObject.SetBool("someBool"false);
someObject.SetFloat("someFloat"1.0);
someObject.SetInt("someInt"2);
someObject.SetString("someString""three");
someObject.SetNull("someNull");

delete someObject


Create a JSON array

PHP Code:

JSONArray someArray = new JSONArray();

someArray.PushBool(false);
someArray.PushFloat(1.0);
someArray.PushInt(2);
someArray.PushString("three");
someArray.PushNull();

delete someArray

You can use the Set*() methods if you need to replace values at a specific index in the array.


Nest JSON

PHP Code:

someObject.Set("someArray"someArray);

JSONArray someArrayCopy view_as<JSONArray>(someObject.Get("someArray"));

delete someArrayCopy


Export to and import from files and strings

PHP Code:

char json[1024];
someObject.ToString(jsonsizeof(json));


char path[PLATFORM_MAX_PATH];
BuildPath(Path_SMpathsizeof(path), "data/array.json");

JSONArray someArray JSONArray.FromFile(path); 

See the encoding flags at the top of json.inc if you want to format the output.


Iterate over the keys in a JSON object

PHP Code:

JSONObjectKeys keys someObject.Keys();
char key[64];

while (
keys.ReadKey(keysizeof(key))) {
    
PrintToServer("%s"key);
}

delete keys


HTTP

Retrieve an item

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
HTTPRequest request = new HTTPRequest("https://jsonplaceholder.typicode.com/todos/1");

    
request.Get(OnTodoReceived);
}

void OnTodoReceived(HTTPResponse responseany value)
{
    if (
response.Status != HTTPStatus_OK) {
        
// Failed to retrieve todo
        
return;
    }

    
// Indicate that the response contains a JSON object
    
JSONObject todo view_as<JSONObject>(response.Data);

    
char title[256];
    
todo.GetString("title"titlesizeof(title));

    
PrintToServer("Retrieved todo with title '%s'"title);



Retrieve a collection

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
HTTPRequest request = new HTTPRequest("https://jsonplaceholder.typicode.com/todos");

    
request.Get(OnTodosReceived);
}

void OnTodosReceived(HTTPResponse responseany value)
{
    if (
response.Status != HTTPStatus_OK) {
        
// Failed to retrieve todos
        
return;
    }

    
// Indicate that the response contains a JSON array
    
JSONArray todos view_as<JSONArray>(response.Data);
    
int numTodos todos.Length;

    
JSONObject todo;
    
char title[256];

    for (
int i 0numTodosi++) {
        
todo view_as<JSONObject>(todos.Get(i));

        
todo.GetString("title"titlesizeof(title));

        
PrintToServer("Retrieved todo with title '%s'"title);

        
// Get() creates a new handle, so delete it when you are done with it
        
delete todo;
    }



Create an item

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
JSONObject todo = new JSONObject();
    
todo.SetBool("completed"false);
    
todo.SetInt("userId"1);
    
todo.SetString("title""foo");

    
HTTPRequest request = new HTTPRequest("https://jsonplaceholder.typicode.com/todos");
    
request.Post(todoOnTodoCreated);

    
// JSON objects and arrays must be deleted when you are done with them
    
delete todo;
}

void OnTodoCreated(HTTPResponse responseany value)
{
    if (
response.Status != HTTPStatus_Created) {
        
// Failed to create todo
        
return;
    }

    
JSONObject todo view_as<JSONObject>(response.Data);

    
PrintToServer("Created todo with ID %d"todo.GetInt("id"));



Update an item

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
JSONObject todo = new JSONObject();
    
todo.SetBool("completed"true);

    
HTTPRequest request = new HTTPRequest("https://jsonplaceholder.typicode.com/todos/1");
    
// Some APIs replace the entire object when using Put,
    // in which case you need to use Patch instead.
    
httpClient.Put(todoOnTodoUpdated);

    
delete todo;
}

void OnTodoUpdated(HTTPResponse responseany value)
{
    if (
response.Status != HTTPStatus_OK) {
        
// Failed to update todo
        
return;
    }

    
JSONObject todo view_as<JSONObject>(response.Data);

    
PrintToServer("Updated todo with ID %d"todo.GetInt("id"));



Delete an item

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
HTTPRequest request = new HTTPRequest("https://jsonplaceholder.typicode.com/todos/1");

    
request.Delete(OnTodoDeleted);
}

void OnTodoDeleted(HTTPResponse responseany value)
{
    if (
response.Status != HTTPStatus_OK) {
        
// Failed to delete todo
        
return;
    }

    
PrintToServer("Deleted todo");


Append query parameters to the URL

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
HTTPRequest request = new HTTPRequest("https://jsonplaceholder.typicode.com/todos");

    
request.AppendQueryParam("userId""%d"1);


The parameter name and value are automatically URL encoded.


Set the credentials for HTTP Basic authentication

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
HTTPRequest request = new HTTPRequest("https://nghttp2.org/httpbin/basic-auth/username/password");

    
request.SetBasicAuth("username""password");


Set request headers

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
HTTPRequest request = new HTTPRequest("https://nghttp2.org/httpbin/bearer");

    
request.SetHeader("Authorization""Bearer %s""some-token");


Get response headers

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
HTTPRequest request = new HTTPRequest("https://nghttp2.org/httpbin/get");

    
request.Get(OnTodosReceived);
}

void OnTodosReceived(HTTPResponse responseany value)
{
    
char date[30];
    
response.GetHeader("Date"datesizeof(date));

    
PrintToServer("Date: %s"date);



Resource methodmaps

You can also write your own methodmaps which inherit from JSONObject, and which abstract away the fields of a resource. This makes your code cleaner when reading and writing resources.


plugin.inc

PHP Code:

methodmap Todo JSONObject
{
    
// Constructor
    
public Todo() { return view_as<Todo>(new JSONObject()); }

    public 
void GetTitle(char[] bufferint maxlength)
    {
        
this.GetString("title"buffermaxlength);
    }
    public 
void SetTitle(const char[] value)
    {
        
this.SetString("title"value);
    }

    
property bool Completed {
        public 
get() { return this.GetBool("completed"); }
        public 
set(bool value) { this.SetBool("completed"value); }
    }
    
property int Id {
        public 
get() { return this.GetInt("id"); }
    }
    
property int UserId {
        public 
get() { return this.GetInt("userId"); }
        public 
set(int value) { this.SetInt("userId"value); }
    }
}; 


plugin.sp

PHP Code:

#include <sourcemod>
#include <ripext>
#include <plugin>

public void OnPluginStart()
{
    
Todo todo = new Todo();
    
todo.Completed false;
    
todo.UserId 1;
    
todo.SetTitle("foo");

    
HTTPRequest request = new HTTPRequest("https://jsonplaceholder.typicode.com/todos");
    
request.Post(todoOnTodoCreated);

    
delete todo;
}

void OnTodoCreated(HTTPResponse responseany value)
{
    if (
response.Status != HTTPStatus_Created) {
        
// Failed to create todo
        
return;
    }

    
Todo todo view_as<Todo>(response.Data);

    
PrintToServer("Todo created with ID %d"todo.Id);



Files

Download a file

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
char imagePath[PLATFORM_MAX_PATH];
    
BuildPath(Path_SMimagePathsizeof(imagePath), "data/image.jpg");

    
HTTPRequest request = new HTTPRequest("https://nghttp2.org/httpbin/image/jpeg");
    
request.DownloadFile(imagePathOnImageDownloaded);
}

void OnImageDownloaded(HTTPStatus statusany value)
{
    if (
status != HTTPStatus_OK) {
        
// Download failed
        
return;
    }

    
PrintToServer("Download complete");



Upload a file

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
char imagePath[PLATFORM_MAX_PATH];
    
BuildPath(Path_SMimagePathsizeof(imagePath), "data/image.jpg");

    
HTTPRequest request = new HTTPRequest("https://example.com/upload");
    
request.UploadFile(imagePathOnImageUploaded);
}

void OnImageUploaded(HTTPStatus statusany value)
{
    if (
status != HTTPStatus_OK) {
        
// Upload failed
        
return;
    }

    
PrintToServer("Upload complete");



Forms

Post form data

PHP Code:

#include <sourcemod>
#include <ripext>

public void OnPluginStart()
{
    
HTTPRequest request = new HTTPRequest("https://nghttp2.org/httpbin/post");

    
request.AppendFormParam("title""%s""foo");
    
request.PostForm(OnFormPosted);
}

void OnFormPosted(HTTPResponse responseany value)
{
    if (
response.Status != HTTPStatus_OK) {
        
// Failed to post form
        
return;
    }

    
// The JSON response data can be retrieved here



good_live 05-30-2017 05:44

Re: REST in Pawn
 
Nice one :)

Deathknife 05-30-2017 06:53

Re: REST in Pawn
 
Sweet.

Quote:

Ability to set request headers and get response headers

Mitchell 05-30-2017 13:18

Re: REST in Pawn
 
Could be used with something like https://firebase.google.com/

splewis 05-30-2017 23:01

Re: REST in Pawn
 
This looks really nice. I've been using steamworks+smjansson for things, this might be able to replace both for me.

ofir753 05-31-2017 07:16

Re: REST in Pawn
 
Looks amazing, will use for sure!

axiom123 06-24-2017 06:06

Re: REST in Pawn - HTTP and JSON natives
 
How to return information from https? use post

ezio_auditore 07-22-2017 14:20

Re: REST in Pawn - HTTP and JSON methodmaps
 
Just a suggestion.

You can also add a function that generates the Authorization header... :D

jdlovins 07-27-2017 11:26

Re: REST in Pawn - HTTP and JSON methodmaps
 
Quick question,

Do i have to delete response.Data after im done with the data or does that get auto cleaned up? In your examples you only delete the handles spawned by looping through an array.

Thanks!

edit:

looking at the source,

Code:

handlesys->FreeHandle(hndlResponse, &sec);
handlesys->FreeHandle(response.hndlData, &sec);

I guess this takes care of the JSON response and the actual response handles?

DJ Tsunami 07-27-2017 13:24

Re: REST in Pawn - HTTP and JSON methodmaps
 
Quote:

Originally Posted by ezio_auditore (Post 2536977)
You can also add a function that generates the Authorization header... :D

Great idea, but I would need to add hashing to this extension, which would make it more bloated. I'll think about it.

Quote:

Originally Posted by jdlovins (Post 2538125)
I guess this takes care of the JSON response and the actual response handles?

Correct, you don't have to worry about cleaning up response and response.Data.

jdlovins 07-27-2017 14:49

Re: REST in Pawn - HTTP and JSON methodmaps
 
Quote:

Originally Posted by DJ Tsunami (Post 2538143)
Great idea, but I would need to add hashing to this extension, which would make it more bloated. I'll think about it.


Correct, you don't have to worry about cleaning up response and response.Data.


Awesome. Something that would be very useful is a URL encode function. For example if you call an API with parameters such as

/items?type=5&name=i have spaces

it will error out on the API end because of the spaces. Not sure how easy that is to solve really.

DarkDeviL 07-27-2017 15:29

Re: REST in Pawn - HTTP and JSON methodmaps
 
Quote:

Originally Posted by jdlovins (Post 2538154)
Awesome. Something that would be very useful is a URL encode function. For example if you call an API with parameters such as

/items?type=5&name=i have spaces

it will error out on the API end because of the spaces. Not sure how easy that is to solve really.

"/items?type=5&name=i+have+spaces"

As this thing seems to be aimed at developers making their own things, I assume you're developing your own stuff that uses this thing, which means you could (remember code attribution!) "copy" this one:

From Dynamic MOTD by @psychonic:

UrlEncodeString

jdlovins 07-27-2017 16:39

Re: REST in Pawn - HTTP and JSON methodmaps
 
Quote:

Originally Posted by arne1288 (Post 2538164)
"/items?type=5&name=i+have+spaces"

As this thing seems to be aimed at developers making their own things, I assume you're developing your own stuff that uses this thing, which means you could (remember code attribution!) "copy" this one:

From Dynamic MOTD by @psychonic:

UrlEncodeString


Correct, making an API and a plugin to use it. Hmm yeah i guess this would solve most things. Would be maybe nice to bake it into the extension since its focused around making web calls :)

DJ Tsunami 07-27-2017 16:56

Re: REST in Pawn - HTTP and JSON methodmaps
 
Looks like there's a curl_easy_escape(), so shouldn't be that hard.

jdlovins 07-27-2017 17:57

Re: REST in Pawn - HTTP and JSON methodmaps
 
Quote:

Originally Posted by DJ Tsunami (Post 2538186)
Looks like there's a curl_easy_escape(), so shouldn't be that hard.

Oh hey there is. That would be pretty awesome if you exposed it :)

jdlovins 07-28-2017 22:37

Re: REST in Pawn - HTTP and JSON methodmaps
 
Sorry for the double post but...

I have a bug and i dont really even know how to describe it in words easily since I cant make a test plugin to replicate it but its definitely not working in the plugin i want it to.

Could i add you on steam DJ Tsunami?

Thanks

Drixevel 07-29-2017 08:09

Re: REST in Pawn - HTTP and JSON methodmaps
 
Any chance of functions to iterate through objects and arrays?

Deathknife 07-29-2017 08:52

Re: REST in Pawn - HTTP and JSON methodmaps
 
Quote:

Originally Posted by Drixevel (Post 2538512)
Any chance of functions to iterate through objects and arrays?

You can already iterate through arrays. Although objects would be nice. smjansson extension allows you to.

PHP Code:

property int Length {
        public 
native get();



DJ Tsunami 07-29-2017 11:22

Re: REST in Pawn - HTTP and JSON methodmaps
 
I intentionally left out object iteration to keep the code simple. Besides, when you're talking to an API you should know which fields are returned. If you're not talking to an API, I believe StringMap should suffice.

Quote:

Originally Posted by jdlovins (Post 2538456)
Could i add you on steam DJ Tsunami?

I'm never on Steam anymore, but I'll try to be on IRC in the coming days. You can also create an issue on GitHub with screenshots and what not.

milutinke 07-30-2017 11:28

Re: REST in Pawn - HTTP and JSON methodmaps
 
Good job :D

Dreizehnt 08-14-2017 12:18

Re: REST in Pawn - Communicate with JSON REST APIs
 
Hi, I have a problem:
HTML Code:

L 08/14/2017 - 19:07:16: [RIPEXT] HTTP request failed: Error reading ca cert file /etc/ssl/certs/ca-certificates.crt - mbedTLS: (-0x3E00) PK - Read/write of file failed
Technical support for hosting where I rent the game server states that the extension visits the directories above the server folder and should not do this. How to be? And what actions are performed in the folder: /etc/ssl/certs/

klippy 08-14-2017 13:00

Re: REST in Pawn - Communicate with JSON REST APIs
 
It's because SSL_CERT_FILE environment variable is set to that path (which your user doesn't have permission to read). I guess you are trying to access a HTTPS endpoint? However, I don't know a fix. I'm sure you could Google that errors because it's a cURL error, it doesn't come from the extension.

DJ Tsunami 08-31-2017 03:57

Re: REST in Pawn - Communicate with JSON REST APIs
 
Version 1.0.3 now comes with the CA bundle in the configs/ripext folder, instead of it being hardcoded to /etc/ssl/certs/ca-certificates.crt. This fixes the issue above and also allows you to replace it with your own CA bundle if your API isn't trusted.

rio_ 09-02-2017 02:11

Re: REST in Pawn - Communicate with JSON REST APIs
 
Is there no way to see if a key exists in a JSONObject? Even GetString produces an exception if the key doesn't exist, and only returns false if the key is set but is null. I feel like that's a huge piece that's missing... It shouldn't be up to the API you're communicating with to insert a null value for every possible key it could return.

asherkin 09-02-2017 04:32

Re: REST in Pawn - Communicate with JSON REST APIs
 
Hmm, that behaviour is wrong according to the SourceMod error model - errors should always be avoidable and thus always caused by programmer error (either due to misuse or missing checks).

DJ Tsunami 09-03-2017 05:42

Re: REST in Pawn - Communicate with JSON REST APIs
 
Quote:

Originally Posted by rio_ (Post 2545934)
It shoudn't be up to the API you're communicating with to insert a null value for every possible key it could return.

I agree, it shouldn't. You should know which fields are returned based on the API's documentation. And APIs should be versioned so that changes don't break existing calls.

Quote:

Originally Posted by asherkin (Post 2545947)
errors should always be avoidable and thus always caused by programmer error (either due to misuse or missing checks).

I'm not sure what's considered "programmer error" in this case. If you pass an invalid index to an ADT array it throws an error, but if you pass an invalid key to an ADT trie it returns false. So the former is programmer error, but the latter is not?

splewis 09-04-2017 14:17

Re: REST in Pawn - Communicate with JSON REST APIs
 
Quote:

Originally Posted by DJ Tsunami (Post 2546161)
I'm not sure what's considered "programmer error" in this case. If you pass an invalid index to an ADT array it throws an error, but if you pass an invalid key to an ADT trie it returns false. So the former is programmer error, but the latter is not?

Given an arbitrary adt array it's possible to prevent going out of bounds with a Length check.

Given an arbitrary adt trie it's not possible to prevent a lookup to a non-existing key based on any check - thus the lookups need to be safe.

Even without that the lack of a "key existence check", most languages don't let you index to random parts of dynamic arrays (exception / undefined behavior), but most will let you attempt lookups to non-existent keys in maps.

asherkin 09-04-2017 14:24

Re: REST in Pawn - Communicate with JSON REST APIs
 
Quote:

Originally Posted by splewis (Post 2546549)
Given an arbitrary adt array it's possible to prevent going out of bounds with a Length check.

Given an arbitrary adt trie it's not possible to prevent a lookup to a non-existing key based on any check - thus the lookups need to be safe.

This is exactly it.

(Yes, a KeyExists function and throwing on invalid keys would also be suitable for the error model, but you pay the hash and lookup penalty twice, so it is simpler in that case to do in one go.)

KyleS 09-05-2017 03:01

Re: REST in Pawn - Communicate with JSON REST APIs
 
https://sm.alliedmods.net/api/index....d=show&id=692&

The ADT_Array API doesn't support this behaviour either (see how with GetTrieValue the return is the state of the key, and the param is actually optional (well; should be)).

Anyways; yeah, in a bunch of plugins I do similar things with checking keys; this is definitely the more intuitive approach then erroring out if a single json key is missing (is this really the point of contention???).

KyleS 09-05-2017 03:01

Re: REST in Pawn - Communicate with JSON REST APIs
 
https://sm.alliedmods.net/api/index....d=show&id=692&

The ADT_Array API doesn't support this behaviour either (see how with GetTrieValue the return is the state of the key, and the param is actually optional (well; should be)).

Anyways; yeah, in a bunch of plugins I do similar things with checking keys; this is definitely the more intuitive approach then erroring out if a single json key is missing (is this really the point of contention???).

rio_ 09-06-2017 01:02

Re: REST in Pawn - Communicate with JSON REST APIs
 
Accelerator is telling me ripext is causing server crashes, can anyone confirm I'm reading this correctly? https://crash.limetech.org/yeyj445f3wip

Or could it be a false-positive? Either way, I have 7 crash reports like that from today. It says something about threads so HTTPClient must be causing it rather than the JSON stuff.

KyleS 09-06-2017 13:06

Re: REST in Pawn - Communicate with JSON REST APIs
 
Quote:

Originally Posted by rio_ (Post 2546966)
Accelerator is telling me ripext is causing server crashes, can anyone confirm I'm reading this correctly? https://crash.limetech.org/yeyj445f3wip

Or could it be a false-positive? Either way, I have 7 crash reports like that from today. It says something about threads so HTTPClient must be causing it rather than the JSON stuff.

Yes, you're not the only person to ask/show that this extension is still very crashy.

Mitchell 09-07-2017 11:12

Re: REST in Pawn - Communicate with JSON REST APIs
 
Quote:

Originally Posted by rio_ (Post 2546966)
Accelerator is telling me ripext is causing server crashes, can anyone confirm I'm reading this correctly? https://crash.limetech.org/yeyj445f3wip

Or could it be a false-positive? Either way, I have 7 crash reports like that from today. It says something about threads so HTTPClient must be causing it rather than the JSON stuff.

Could you explain what you're doing for these crashes? I was planning on doing just sending POST data to a java server, however this kind of stumps the idea if it's crashing the server.

edit: also what SM version were you using?

rio_ 09-07-2017 17:16

Re: REST in Pawn - Communicate with JSON REST APIs
 
I have a single HTTPClient with no extra headers and I'm making several GET requests at a time periodically, maybe something like every few minutes on average. The crashes are relatively infrequent relative to the amount of requests being made.

SM version is 1.9.0.6080.

Mitchell 09-07-2017 17:22

Re: REST in Pawn - Communicate with JSON REST APIs
 
Possible reference to 1.9 and crashes: https://forums.alliedmods.net/showpo...&postcount=324
However I don't know too much about this extension or what was added/changed in 1.9

Mitchell 10-01-2017 13:13

Re: REST in Pawn - Communicate with JSON REST APIs
 
I think there's a leak within closing the HTTPClientObjects: (ignore the trie snapshot leak i've fixed it already)
Code:

L 09/23/2017 - 17:55:44: [SM] Unloading plugin to free 24279 handles.
L 09/23/2017 - 17:55:44: [SM] Contact the author(s) of this plugin to correct this error.
L 09/23/2017 - 17:55:44: --------------------------------------------------------------------------
L 09/23/2017 - 17:55:44: Type        GlobalFwd          |        Count        1
L 09/23/2017 - 17:55:44: Type        Trie                |        Count        2
L 09/23/2017 - 17:55:44: Type        TrieSnapshot        |        Count        21611
L 09/23/2017 - 17:55:44: Type        HTTPClientObject    |        Count        2652
L 09/23/2017 - 17:55:44: Type        JSONObject          |        Count        13
L 09/23/2017 - 17:55:44: -- Approximately 3379384 bytes of memory are in use by (24279) Handles.

If it helps heres the part of the code im using:
PHP Code:


        HTTPClient hHTTPClient 
= new HTTPClient("http://XXXXXX.xxx.xxx:8080");
        
hHTTPClient.SetHeader("authorization""Basic XXXXXXX");
        
hHTTPClient.Post("roundPost"mainRoundEventOnHTTPResponse);

public 
void OnHTTPResponse(HTTPResponse responseany value) {


Also this fails to connect to the webserver also (because it's not up currently). And the code executes on Round_End

Deathknife 10-01-2017 19:51

Re: REST in Pawn - Communicate with JSON REST APIs
 
Quote:

Originally Posted by Mitchell (Post 2551948)
I think there's a leak within closing the HTTPClientObjects: (ignore the trie snapshot leak i've fixed it already)
Code:

L 09/23/2017 - 17:55:44: [SM] Unloading plugin to free 24279 handles.
L 09/23/2017 - 17:55:44: [SM] Contact the author(s) of this plugin to correct this error.
L 09/23/2017 - 17:55:44: --------------------------------------------------------------------------
L 09/23/2017 - 17:55:44: Type        GlobalFwd          |        Count        1
L 09/23/2017 - 17:55:44: Type        Trie                |        Count        2
L 09/23/2017 - 17:55:44: Type        TrieSnapshot        |        Count        21611
L 09/23/2017 - 17:55:44: Type        HTTPClientObject    |        Count        2652
L 09/23/2017 - 17:55:44: Type        JSONObject          |        Count        13
L 09/23/2017 - 17:55:44: -- Approximately 3379384 bytes of memory are in use by (24279) Handles.

If it helps heres the part of the code im using:
PHP Code:


        HTTPClient hHTTPClient 
= new HTTPClient("http://XXXXXX.xxx.xxx:8080");
        
hHTTPClient.SetHeader("authorization""Basic XXXXXXX");
        
hHTTPClient.Post("roundPost"mainRoundEventOnHTTPResponse);

public 
void OnHTTPResponse(HTTPResponse responseany value) {


Also this fails to connect to the webserver also (because it's not up currently). And the code executes on Round_End

I believe HTTPClient isn't deleted by extension after response, only HTTPResponse and any data objects. HTTPClient is made so it can be re-used if main url is same and headers, and you just do .Post/.Get/etc on it when you want different path and input.
Either you should delete HTTPClient on response, or only form it once per host.

Mitchell 10-01-2017 20:32

Re: REST in Pawn - Communicate with JSON REST APIs
 
Quote:

Originally Posted by Deathknife (Post 2552009)
I believe HTTPClient isn't deleted by extension after response, only HTTPResponse and any data objects. HTTPClient is made so it can be re-used if main url is same and headers, and you just do .Post/.Get/etc on it when you want different path and input.
Either you should delete HTTPClient on response, or only form it once per host.

Ah, the test plugin was a bit misleading then, maybe it should show creating the HTTPClient and storing it as a global Handle etc. Thanks for the info though, plugin was working well while my webserver was up.

DJ Tsunami 10-03-2017 06:12

Re: REST in Pawn - Communicate with JSON REST APIs
 
I think the code examples in the second post are better examples than the test plugin, as they do show the HTTPClient being stored in a global variable, and they are well documented.


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

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