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)

asdfxD 04-30-2021 00:34

Re: REST in Pawn 1.2 - HTTP client for JSON REST APIs (Updated 2021/02/07)
 
i have sv_hibernate_when_empty always at 0, with this extension my server goes in hibernation like -> sv_hibernate_when_empty 1

edit: it's not the extension, its eitems.smx or estickers.smx

DJ Tsunami 06-06-2021 07:35

Re: REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/06/06)
 
Version 1.3 has been released with the following changes:
  • Deprecated HTTPClient methodmap
  • Introduced HTTPRequest methodmap
  • Added ability to append query parameters to the URL
  • Added ability to set the credentials for HTTP Basic authentication
  • Added ability to set the maximum number of redirects to follow
  • Added ability to POST form data
  • Allow user defined 'Accept' and 'Content-Type' headers (thanks Peak)
  • Fixed HTTP/2 timeouts by enforcing HTTP/1.1 for file transfers on Windows
  • Fixed response body being printed to server console during file upload
  • Throw an error when passing empty values to some natives
Download

Deather 06-07-2021 14:07

Re: REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/06/06)
 
Is it possible to log request?

When I try to send some POST data to my API (Laravel), backend receives empty request (headers and method are correct, but there is no POST data).

Code:

    JSONObject BanJson = new JSONObject();
    BanJson.SetString("nickname", Nickname);
    BanJson.SetString("steam_id", SteamId);
    BanJson.SetInt("type", Type);
    BanJson.SetInt("duration", Duration);
    BanJson.SetString("reason", Reason);
    BanJson.SetString("admin_nickname", AdminNickname);
    BanJson.SetString("admin_steam_id", AdminSteamId);

    HTTPRequest Request = new HTTPRequest(GetApiURI(ApiEndPoint));
   
    Request.SetHeader("Authorization", "Bearer %s",  GeneralConfig.Token);
    Request.SetHeader("GameServer", "%d", GeneralConfig.ServerId);
    Request.SetHeader("Content-Type", "application/json");
    Request.SetHeader("Accept", "application/json");

    Request.Post(BanJson, OnPlayerBan);

Same request sended by PostMan works correctly...

DJ Tsunami 06-07-2021 15:40

Re: REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/06/06)
 
How are you reading the POST data in Laravel? This works for me with both Postman and REST in Pawn:

PHP Code:

Route::post('/post', function (Request $request) {
    return new 
JsonResponse($request->all());
}); 


Deather 06-07-2021 16:26

Re: REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/06/06)
 
Laravel:
PHP Code:

Route::post('/test', function (Request $request) {
    return 
response()->json($request->all());
}); 

Simple PHP:
PHP Code:

<?php

$postData 
json_encode($_POST);
$jsonData file_get_contents('php://input');

echo 
$postData;
echo 
$jsonData;

file_put_contents('post.txt'$postData);
file_put_contents('json.txt'$jsonData);

?>

Both solutions receives nothing when requesting from game server.


@edit:
PHP Code:

    Request.AppendFormParam("test""%s""foo");
    
Request.PostForm(OnTestRequest); 

I've used PostForm instead of Post but i still don't know why Post + JSON doesn't work...


@edit-2:
I've saved some backend logs:
PostForm():
PHP Code:

[2021-06-08 14:39:05local.DEBUGContent-Type"application\/x-www-form-urlencoded"  
[2021-06-08 14:39:05local.DEBUGAccept"application\/json"  
[2021-06-08 14:39:05local.DEBUGPath"api\/test2"  
[2021-06-08 14:39:05local.DEBUGMethod"POST"  
[2021-06-08 14:39:05local.DEBUGData: {"test":"foo"}  
[
2021-06-08 14:39:05local.DEBUG$_POST: {"test":"foo"

Post() + JSON
PHP Code:

[2021-06-08 14:41:40local.DEBUGContent-Type"application\/json"  
[2021-06-08 14:41:40local.DEBUGAccept"application\/json"  
[2021-06-08 14:41:40local.DEBUGPath"api\/test2"  
[2021-06-08 14:41:40local.DEBUGMethod"POST"  
[2021-06-08 14:41:40local.DEBUGData: []  
[
2021-06-08 14:41:40local.DEBUG$_POST: [] 

Post() + JSON (Content-Type: application/x-www-form-urlencoded)
PHP Code:

[2021-06-08 14:42:42local.DEBUGContent-Type"application\/x-www-form-urlencoded"  
[2021-06-08 14:42:42local.DEBUGAccept"application\/json"  
[2021-06-08 14:42:42local.DEBUGPath"api\/test2"  
[2021-06-08 14:42:42local.DEBUGMethod"POST"  
[2021-06-08 14:42:42local.DEBUGData: []  
[
2021-06-08 14:42:42local.DEBUG$_POST: [] 

It will be really nice to log what extension sends to backend (to be sure that isn't extension bug)...

DJ Tsunami 06-13-2021 06:01

Re: REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/06/06)
 
4 Attachment(s)
Quote:

Originally Posted by Deather (Post 2749192)
It will be really nice to log what extension sends to backend (to be sure that isn't extension bug)...

I've attached debug builds for Linux and Windows. It would also be useful to know what webserver you're using (Apache / nginx / Caddy?), and which version.

Laravel should automatically fill the Request object if the Content-Type is application/json. PHP's $_POST variable will only be filled when posting form data.

digin 06-19-2021 21:54

Re: REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/06/06)
 
i don't know why but after i move to my new VPS the PUT think is not working, always return HTTPStatus_Invalid = 0.

My Old VPS : Plesk Panel (PHP 7.4.20 + Server API FPM/FastCGI)
My New VPS : Cyberpanel (PHP 7.4.20 + Server API LiteSpeed V7.9)

"REST in Pawn" (1.2.3) by Tsunami: Provides HTTP and JSON natives for plugins

i test with Postman it's working, but the plugin always return HTTPStatus_Invalid = 0.

anyone experiencing the same thing?

digin 06-19-2021 22:33

Re: REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/06/06)
 
i try with Debug builds from you above

PHP Code:

=== InfoSTATEINIT => CONNECT handle 0xde922140line 1646 (connection #-5000)

=== InfoFound bundle for host subdomain.domain.com0xde9070c0 [can multiplex]

=== 
InfoRe-using existing connection! (#0) with host subdomain.domain.com

=== InfoConnected to subdomain.domain.com (194.XXX.XX.XXport 443 (#0)

=== InfoSTATECONNECT => DO handle 0xde922140line 1700 (connection #0)

=== Infohttp2_send len=194

=== Infoh2 header: :method:PUT

=== Infoh2 header: :path:/api/retakes/76561199145573335

=== Infoh2 header: :scheme:https

=== Infoh2 header: :authority:subdomain.domain.com

=== Infoh2 headeruser-agent:sm-ripext/1.3.0

=== Infoh2 headeraccept-encoding:deflategzip

=== Infoh2 headeraccept:application/json

=== Infoh2 headercontent-type:applicaton/json

=== Infohttp2_send request allowed 1 (easy handle 0xde922140)

=== 
InfoUsing Stream ID(easy handle 0xde922140)

==> 
Send header
PUT 
/api/retakes/76561199145573335 HTTP/2

Host
subdomain.domain.com

user
-agentsm-ripext/1.3.0

accept
-encodingdeflategzip

accept
application/json

content
-typeapplicaton/json




=== InfoSTATE: DO => DO_DONE handle 0xde922140line 1935 (connection #0)

=== InfoSTATEDO_DONE => PERFORM handle 0xde922140line 2056 (connection #0)

=== Infohttp2_send len=306

=== Infodata_source_read_callbackreturns 306 bytes stream 3

=== Infohttp2_send returns 306 for stream 3

==> Send data
{"pistolround_ct""weapon_usp_silencer""primary_ct""weapon_ak47""secondary_ct""weapon_usp_silencer""smg_ct""weapon_ump45""awp_ct"0"scout_ct"0"pistolround_t""weapon_glock""primary_t""weapon_ak47""secondary_t""weapon_glock""smg_t""weapon_ump45""awp_t"0"scout_t"0}
=== 
Infoh2_process_pending_inputAll data in connection buffer processed

=== Infodata_source_read_callbackreturns 0 bytes stream 3

=== Infohttp2_recveasy 0xde922140 (stream 3win 33554278/33554432

=== Infonread=13

=== Infoon_frame_recv() header 3 stream 3

=== InfoGot frame type 3 for stream 3!

=== 
Infoon_stream_close(), PROTOCOL_ERROR (err 1), stream 3

=== InfoRemoved stream 3 hash!

=== 
Infoh2_process_pending_inputAll data in connection buffer processed

=== Infohttp2_recveasy 0xde922140 (stream 0win 33554278/4294967295

=== Infoh2_process_pending_inputAll data in connection buffer processed

=== InfoHTTP/2 stream 0 was not closed cleanlyPROTOCOL_ERROR (err 1)
*********&
#1928;***************************    %******************ܠ******************P*********************************************`*************************** ***************************p***************************,***************************H***************************蠓*********************************************|******************************************************<***************************Ǥ*********ވ***************************`***************************@!******************@Ȓ*********X***************************R***************************
=== Infomulti_done

=== Infostopped the pause stream!

=== 
InfoConnection #0 to host subdomain.domain.com left intact

=== InfoExpire cleared (transfer 0xde922140


CrazyHackGUT 08-02-2021 14:26

Re: REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/06/06)
 
It is possible to send non-chunked request body? Sometimes for shared web-hostings it breaks everything, unfortunately.

DJ Tsunami 08-24-2021 11:48

Re: REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/06/06)
 
Quote:

Originally Posted by CrazyHackGUT (Post 2754381)
It is possible to send non-chunked request body? Sometimes for shared web-hostings it breaks everything, unfortunately.

Have you tried
PHP Code:

request.SetHeader("Transfer-Encoding"""); 

? I'm not sure if it will work, since it puts a space after the colon.


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

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