22#include <websocket>
33
44public Plugin myinfo = {
5- name = " HTTP Client Test" ,
6- author = " ProjectSky" ,
7- description = " Used for testing HTTP client" ,
8- version = " 1.0.0" ,
9- url = " https://github.com/ProjectSky/sm-ext-websocket"
5+ name = " HTTP Client Test" ,
6+ author = " ProjectSky" ,
7+ description = " Used for testing HTTP client" ,
8+ version = " 1.0.0" ,
9+ url = " https://github.com/ProjectSky/sm-ext-websocket"
1010}
1111
1212public void OnPluginStart ()
1313{
14- RegServerCmd (" http_get" , http_get );
15- RegServerCmd (" http_post_json" , http_post_json );
16- RegServerCmd (" http_post_form" , http_post_form );
17- RegServerCmd (" http_put" , http_put );
18- RegServerCmd (" http_patch" , http_patch );
19- RegServerCmd (" http_delete" , http_delete );
14+ RegServerCmd (" http_get" , http_get );
15+ RegServerCmd (" http_post_json" , http_post_json );
16+ RegServerCmd (" http_post_form" , http_post_form );
17+ RegServerCmd (" http_put" , http_put );
18+ RegServerCmd (" http_patch" , http_patch );
19+ RegServerCmd (" http_delete" , http_delete );
2020}
2121
2222Action http_get (int args )
2323{
24- HttpRequest request = new HttpRequest (" https://httpbin.org/get" );
25- request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
26- request .Get (OnHttpResponse );
27- return Plugin_Handled ;
24+ HttpRequest request = new HttpRequest (" https://httpbin.org/get" );
25+ request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
26+ request .Get (OnHttpResponse );
27+ return Plugin_Handled ;
2828}
2929
3030Action http_post_json (int args )
3131{
32- HttpRequest request = new HttpRequest (" https://httpbin.org/post" );
33-
34- // Create JSON payload
35- YYJSONObject json = new YYJSONObject ();
36- json .SetString (" name" , " test" );
37- json .SetInt (" age" , 25 );
38- json .SetBool (" active" , true );
39-
40- // Add custom headers
41- request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
42-
43- // Send POST request with JSON body
44- request .PostJson (json , OnHttpResponse );
45-
46- delete json ;
47- return Plugin_Handled ;
32+ HttpRequest request = new HttpRequest (" https://httpbin.org/post" );
33+
34+ // Create JSON payload
35+ YYJSONObject json = new YYJSONObject ();
36+ json .SetString (" name" , " test" );
37+ json .SetInt (" age" , 25 );
38+ json .SetBool (" active" , true );
39+
40+ // Add custom headers
41+ request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
42+
43+ // Send POST request with JSON body
44+ request .PostJson (json , OnHttpResponse );
45+
46+ delete json ;
47+ return Plugin_Handled ;
4848}
4949
5050Action http_post_form (int args )
5151{
52- HttpRequest request = new HttpRequest (" https://httpbin.org/post" );
53-
54- // Add form parameters
55- request .AppendFormParam (" username" , " john_doe" );
56- request .AppendFormParam (" email" , " john@example.com" );
57- request .AppendFormParam (" age" , " 25" );
58-
59- // Add custom headers
60- request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
61-
62- // Send POST request with form data
63- request .PostForm (OnHttpResponse );
64- return Plugin_Handled ;
52+ HttpRequest request = new HttpRequest (" https://httpbin.org/post" );
53+
54+ // Add form parameters
55+ request .AppendFormParam (" username" , " john_doe" );
56+ request .AppendFormParam (" email" , " john@example.com" );
57+ request .AppendFormParam (" age" , " 25" );
58+
59+ // Add custom headers
60+ request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
61+
62+ // Send POST request with form data
63+ request .PostForm (OnHttpResponse );
64+ return Plugin_Handled ;
6565}
6666
6767Action http_put (int args )
6868{
69- HttpRequest request = new HttpRequest (" https://httpbin.org/put" );
70-
71- // Create JSON payload for PUT
72- YYJSONObject json = new YYJSONObject ();
73- json .SetString (" id" , " 123" );
74- json .SetString (" name" , " updated_name" );
75- json .SetBool (" active" , false );
76-
77- // Add custom headers
78- request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
79-
80- // Send PUT request with JSON body
81- request .PutJson (json , OnHttpResponse );
82-
83- delete json ;
84- return Plugin_Handled ;
69+ HttpRequest request = new HttpRequest (" https://httpbin.org/put" );
70+
71+ // Create JSON payload for PUT
72+ YYJSONObject json = new YYJSONObject ();
73+ json .SetString (" id" , " 123" );
74+ json .SetString (" name" , " updated_name" );
75+ json .SetBool (" active" , false );
76+
77+ // Add custom headers
78+ request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
79+
80+ // Send PUT request with JSON body
81+ request .PutJson (json , OnHttpResponse );
82+
83+ delete json ;
84+ return Plugin_Handled ;
8585}
8686
8787Action http_patch (int args )
8888{
89- HttpRequest request = new HttpRequest (" https://httpbin.org/patch" );
90-
91- // Create JSON payload for PATCH
92- YYJSONObject json = new YYJSONObject ();
93- json .SetString (" name" , " partial_update" );
94-
95- // Add custom headers
96- request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
97-
98- // Send PATCH request with JSON body
99- request .PatchJson (json , OnHttpResponse );
100-
101- delete json ;
102- return Plugin_Handled ;
89+ HttpRequest request = new HttpRequest (" https://httpbin.org/patch" );
90+
91+ // Create JSON payload for PATCH
92+ YYJSONObject json = new YYJSONObject ();
93+ json .SetString (" name" , " partial_update" );
94+
95+ // Add custom headers
96+ request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
97+
98+ // Send PATCH request with JSON body
99+ request .PatchJson (json , OnHttpResponse );
100+
101+ delete json ;
102+ return Plugin_Handled ;
103103}
104104
105105Action http_delete (int args )
106106{
107- HttpRequest request = new HttpRequest (" https://httpbin.org/delete" );
108- request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
109- request .Delete (OnHttpResponse );
110- return Plugin_Handled ;
107+ HttpRequest request = new HttpRequest (" https://httpbin.org/delete" );
108+ request .AddHeader (" User-Agent" , " SourceMod HTTP Client" );
109+ request .Delete (OnHttpResponse );
110+ return Plugin_Handled ;
111111}
112112
113113void OnHttpResponse (HttpRequest http , const char [] body , int statusCode , int bodySize , any value )
114114{
115- // Print response details
116- PrintToServer (" Status Code: %d " , statusCode );
117- PrintToServer (" Body Size: %d bytes" , bodySize );
118-
119- // Print response headers
120- char headerValue [256 ];
121- if (http .GetResponseHeader (" Content-Type" , headerValue , sizeof (headerValue )))
122- {
123- PrintToServer (" Content-Type: %s " , headerValue );
124- }
125-
126- // Print response body
127- PrintToServer (" Response Body: %s " , body );
115+ // Print response details
116+ PrintToServer (" Status Code: %d " , statusCode );
117+ PrintToServer (" Body Size: %d bytes" , bodySize );
118+
119+ // Print response headers
120+ char headerValue [256 ];
121+ if (http .GetResponseHeader (" Content-Type" , headerValue , sizeof (headerValue )))
122+ {
123+ PrintToServer (" Content-Type: %s " , headerValue );
124+ }
125+
126+ // Print response body
127+ PrintToServer (" Response Body: %s " , body );
128128}
0 commit comments