/
HttpService.yaml
537 lines (493 loc) · 19.9 KB
/
HttpService.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
name: HttpService
type: class
category: Data
memory_category: Instances
summary: |
Allows sending HTTP requests and provides various web-related functions,
including JSON functions.
description: |
**HttpService** allows HTTP requests to be sent from game servers using
`Class.HttpService:RequestAsync()|RequestAsync`,
`Class.HttpService:GetAsync()|GetAsync` and
`Class.HttpService:PostAsync()|PostAsync`. This service allows games to be
integrated with off-Roblox web services such as analytics, data storage,
remote server configuration, error reporting, advanced calculations or
real-time communication.
You should only send HTTP requests to trusted third-party platforms to avoid
making your experience vulnerable to security risks.
## Enabling HTTP Requests
Request-sending functions aren't enabled by default: attempting to use them
while disabled will result in the error "Http requests are not enabled. Enable
via game settings". To send requests, set
`Class.HttpService.HttpEnabled|HttpEnabled` to true through the Game Settings
interface in Studio under the Security section, or use the Command Bar for
unpublished experiences. This property cannot be interacted with at runtime.
```lua
-- For unpublished games, use this in the Command Bar:
game:GetService("HttpService").HttpEnabled = true
```
## Other Functions
HttpService also houses the `Class.HttpService:JSONEncode()|JSONEncode` and
`Class.HttpService:JSONDecode()|JSONDecode` functions, which are useful for
communicating with services that use the [JSON](https://json.org) format. In
addition, the `Class.HttpService:GenerateGUID()|GenerateGUID` function
provides random 128-bit labels, which can be treated as probabilistically
unique in a variety of scenarios.
## Use in Plugins
HttpService can also be used by Roblox Studio plugins. They may do this to
check for updates, send usage data, download content or other business logic.
The first time a plugin attempts to do this, the user may be prompted to give
the plugin permission to communicate with the particular web address. A user
may accept, deny and revoke such permissions at any time through the Plugin
Management window.
Plugins may also communicate with other software running on the same computer
through the `localhost` and `127.0.0.1` hosts. By running programs compatible
with such plugins, you can extend the functionality of your plugin beyond the
normal capabilities of Roblox Studio, such as interacting with your computer's
file system. Beware that such software must be distributed separately from the
plugin itself, and can pose security hazards if you aren't careful.
## Considerations
- There are port restrictions. You cannot use port 1194 or any port below
1024, except 80 and 443. If you try to use a blocked port, you will receive
either a `403 Forbidden` or `ERR_ACCESS_DENIED` error.
- For each Roblox game server, there is a limit of 500 HTTP requests per
minute. Exceeding this may cause request-sending functions to stall entirely
for about 30 seconds.
- Requests cannot be made to any Roblox website, such as www.roblox.com.
- Web requests can fail for many reasons, so it is important to "code
defensively" (use `pcall`) and have a plan for when requests fail.
- Although the `http://` protocol is supported, you should use `https://`
wherever possible.
- Requests sent should provide a secure form of authentication, such as a
pre-shared secret key, so that bad actors cannot pose as one of your Roblox
game servers.
- Be aware of the general capacity and rate-limiting policies of the web
servers to which requests are being sent.
code_samples:
- Astronauts-in-Space
- International-Space-Station
- HttpService-Pastebin
inherits:
- Instance
tags:
- NotCreatable
- Service
deprecation_message: ''
properties:
- name: HttpService.HttpEnabled
summary: |
Indicates whether http request can be sent to external websites.
description: |
When set to true, you are able to send requests to other websites using
`Class.HttpService:GetAsync()`, `Class.HttpService:PostAsync()`, and
`Class.HttpService:RequestAsync()`.
To enable HTTP requests, the `Class.HttpService` must be enabled in
Studio's [Game Settings](../../../studio/game-settings.md) through the
**Security** → **Allow HTTP Requests** toggle.
code_samples:
type: bool
tags: []
deprecation_message: ''
security:
read: None
write: LocalUserSecurity
thread_safety: ReadSafe
category: Data
serialization:
can_load: true
can_save: true
methods:
- name: HttpService:GenerateGUID
summary: |
Generates a UUID/GUID random string, optionally with curly braces.
description: |
The GenerateGUID function randomly creates a [universally unique
identifier (UUID)][1] string.
The sixteen octets of a UUID are represented as 32 hexadecimal (base 16)
digits, displayed in 5 groups separated by hyphens in the form
`8-4-4-4-12` for a total of 36 characters. For example:
`123e4567-e89b-12d3-a456-426655440000`.
The _wrapInCurlyBraces_ argument determines whether the returned string is
wrapped in curly braces `{}`. For instance:
- `true` - `{94b717b2-d54f-4340-a504-bd809ef5bf5c}`
- `false` - `db454790-7563-44ed-ab4b-397ff5df737b`
[1]: https://en.wikipedia.org/wiki/Universally_unique_identifier
code_samples:
- HttpService-GenerateGUID
parameters:
- name: wrapInCurlyBraces
type: bool
default: true
summary: |
Whether the returned string should be wrapped in _{curly braces}_.
returns:
- type: string
summary: |
The randomly generated UUID.
tags: []
deprecation_message: ''
security: None
thread_safety: Safe
- name: HttpService:GetSecret
summary: |
Returns a `Datatype.Secret` from the secrets store.
description: |
This method returns a value previously added to the secrets store for the
experience. The secret content is not printable and, for security reasons,
secrets are not available when the experience runs locally.
The returned `Datatype.Secret` can be transformed using built-in methods
such as `Datatype.Secret:AddPrefix()`. It is expected to be sent as a part
of the HTTP request.
Note that a secret may have an associated domain name. If the domain name
is empty, the secret cannot be sent over the network and therefore could
not be used as an API key. To allow sending the secret anywhere, set the
domain name to `*`. You can also limit the URL to a specific domain name
such as `apis.myservice.com` or to subdomains of a domain, for example
`*.myservice.org`.
code_samples: []
parameters:
- name: key
type: string
default:
summary: ''
returns:
- type: Secret
summary: ''
tags: []
deprecation_message: ''
security: None
thread_safety: Safe
- name: HttpService:JSONDecode
summary: |
Decodes a JSON string into a Lua table.
description: |
The JSONDecode function transforms a JSON object or array into a Lua
[table](../../../luau/tables.md) with the following characteristics:
- Keys of the table are strings or numbers but not both. If a JSON object
contains both, string keys are ignored.
- An empty JSON object generates an empty Lua table `{}`.
- If the _input_ string is not a valid JSON object, this function will
throw an error.
To encode a Lua table into a JSON object, you can use the
`Class.HttpService:JSONEncode()` function.
Many web endpoints use JSON, as it is commonly used on the Internet. Visit
[JSON.org](https://www.json.org) to become more familiar with the format.
This method can be used regardless of whether HTTP Requests are
`Class.HttpService.HttpEnabled|enabled`.
code_samples:
- HttpService-JSONDecode
parameters:
- name: input
type: string
default:
summary: |
The JSON object being decoded.
returns:
- type: Variant
summary: |
The decoded JSON object as a Lua table.
tags:
- CustomLuaState
deprecation_message: ''
security: None
thread_safety: Safe
- name: HttpService:JSONEncode
summary: |
Generate a JSON string from a Lua table.
description: |
The JSONEncode function transforms a Lua [table](../../../luau/tables.md)
into a JSON object or array based on the following guidelines:
- Keys of the table must be either strings or numbers. If a table contains
both, an array takes priority (string keys are ignored).
- An empty Lua table `{}` generates an empty JSON array.
- The value `nil` is never generated.
- Cyclic table references cause an error.
This function allows values such as `inf` and `nan`, which are not valid
JSON. This may cause problems if you want to use the outputted JSON
elsewhere.
To reverse the encoding process, and decode a JSON object, you can use the
`Class.HttpService:JSONDecode()` function.
Many web endpoints use JSON, as it is commonly used on the Internet. Visit
[JSON.org](https://www.json.org) to become more familiar with the format.
This method can be used regardless of whether HTTP Requests are
`Class.HttpService.HttpEnabled|enabled`.
code_samples:
- HttpService-JSONEncode
parameters:
- name: input
type: Variant
default:
summary: |
The input Lua table.
returns:
- type: string
summary: |
The returned JSON string.
tags:
- CustomLuaState
deprecation_message: ''
security: None
thread_safety: Safe
- name: HttpService:UrlEncode
summary: |
Replaces URL-unsafe characters with '%' and two hexadecimal characters.
description: |
The UrlEncode function [percent-encodes][1] a given string so that
reserved characters properly encoded with '%' and two hexadecimal
characters.
This is useful when formatting URLs for use with
`Class.HttpService:GetAsync()`/`Class.HttpService:PostAsync()`, or POST
data of the media type `application/x-www-form-urlencoded`
(`Enum.HttpContentType|Enum.HttpContentType.ApplicationUrlEncoded`).
For instance, when you encode the URL
` https://www.roblox.com/discover#/`, this function returns
`https%3A%2F%2Fwww%2Eroblox%2Ecom%2Fdiscover%23%2F`.
[1]: https://en.wikipedia.org/wiki/Percent-encoding
code_samples:
- HttpService-UrlEncode
- HttpService-Pastebin
parameters:
- name: input
type: string
default:
summary: |
The string (URL) to encode.
returns:
- type: string
summary: |
The encoded string.
tags: []
deprecation_message: ''
security: None
thread_safety: Safe
- name: HttpService:GetAsync
summary: |
Send an HTTP GET request.
description: |
The GetAsync function sends an HTTP GET request. It functions similarly to
`Class.HttpService:RequestAsync()|RequestAsync` except that it accepts
HTTP request parameters as method parameters instead of a single
dictionary and returns only the body of the HTTP response. Generally, this
method is useful only as a shorthand and
`Class.HttpService:RequestAsync()|RequestAsync` should to be used in most
cases.
When true, the `nocache` parameter prevents this function from caching
results from previous calls with the same `url`.
code_samples:
- Astronauts-in-Space
- International-Space-Station
parameters:
- name: url
type: Variant
default:
summary: |
The web address you are requesting data from.
- name: nocache
type: bool
default: false
summary: |
Whether the request stores (caches) the response.
- name: headers
type: Variant
default:
summary: |
Used to specify some HTTP request headers.
returns:
- type: string
summary: |
The GET request's response body.
tags:
- Yields
deprecation_message: ''
security: None
thread_safety: Unsafe
- name: HttpService:PostAsync
summary: |
Performs a HTTP POST request.
description: |
The PostAsync function sends an HTTP POST request. It functions similarly
to `Class.HttpService:RequestAsync()|RequestAsync` except that it accepts
HTTP request parameters as method parameters instead of a single
dictionary and returns only the body of the HTTP response. Generally, this
method is useful only as a shorthand and
`Class.HttpService:RequestAsync()|RequestAsync` should to be used in most
cases.
When true, the `compress` parameter controls whether large request bodies
will be compressed using gzip.
code_samples:
- HttpService-Pastebin
parameters:
- name: url
type: Variant
default:
summary: |
The destination address for the data.
- name: data
type: string
default:
summary: |
The data being sent.
- name: content_type
type: HttpContentType
default: ApplicationJson
summary: |
Modifies the value in the _Content-Type_ header sent with the request.
- name: compress
type: bool
default: false
summary: |
Determines whether the data is compressed (gzipped) when sent.
- name: headers
type: Variant
default:
summary: |
Used to specify some HTTP request headers.
returns:
- type: string
summary: |
The HTTP response sent back indicating the request result.
tags:
- Yields
deprecation_message: ''
security: None
thread_safety: Unsafe
- name: HttpService:RequestAsync
summary: |
Sends an HTTP request using any HTTP method given a dictionary of
information.
description: |
This method sends an HTTP request using a dictionary to specify the
request data, such as the target URL, method, headers and request body
data. It returns a dictionary that describes the response data received.
Optionally, the request can be compressed using
`Enum.HttpCompression|HttpCompression`.
#### Request Dictionary Fields
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Required</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Url</b></td>
<td>String</td>
<td>yes</td>
<td>The target URL for this request. Must use <code>http</code> or <code>https</code> protocols.</td>
</tr>
<tr>
<td><b>Method</b></td>
<td>String</td>
<td>no</td>
<td>The HTTP method being used by this request, most often GET or POST.</td>
</tr>
<tr>
<td><b>Headers</b></td>
<td>Dictionary</td>
<td>no</td>
<td>A dictionary of headers to be used with this request. Most HTTP headers are accepted here, but not all.</td>
</tr>
<tr>
<td><b>Body</b></td>
<td>String</td>
<td>no</td>
<td>The request body. Can be any string, including binary data. Must be excluded when using the GET or HEAD HTTP methods. It might be necessary to specify the <code>Content-Type</code> header when sending JSON or other formats.</td>
</tr>
<tr>
<td><b>Compress</b></td>
<td><code>Enum.HttpCompression</code></td>
<td>no</td>
<td>An optional compression field that will compress the data in the request. The value can either be <code>Enum.HttpCompression|None</code> or <code>Enum.HttpCompression|Gzip</code>.</td>
</tr>
</tbody>
</table>
#### HTTP Headers
In the request dictionary, you can specify custom HTTP headers to use in
the request. However, some headers cannot be specified. For example,
`Content-Length` is determined from the request body. `User-Agent` and
`Roblox-Id` are locked by Roblox. Other headers like `Accept` or
`Cache-Control` use default values but can be overridden. More commonly,
some REST APIs may require API keys or other service authentication to be
specified in request headers.
This method does not detect the format of body content. Many web servers
require the `Content-Type` header be set appropriately when sending
certain formats. Other methods of `Class.HttpService` use the
`Enum.HttpContentType` enum; for this method set the `Content-Type` header
appropriately: `text/plain`, `text/xml`, `application/xml`,
`application/json` or `application/x-www-form-urlencoded` are replacement
`Content-Type` header values for the respective enum values.
#### Response Dictionary Fields
The function returns a dictionary containing the following fields:
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Success</b></td>
<td>Boolean</td>
<td>The success status of the request. This is true if and only if the <b>StatusCode</b> lies within the range [200, 299].</td>
</tr>
<tr>
<td><b>StatusCode</b></td>
<td>Integer</td>
<td>The HTTP response code identifying the status of the response.</td>
</tr>
<tr>
<td><b>StatusMessage</b></td>
<td>String</td>
<td>The status message that was sent back.</td>
</tr>
<tr>
<td><b>Headers</b></td>
<td>Dictionary</td>
<td>A dictionary of headers that were set in this response.</td>
</tr>
<tr>
<td><b>Body</b></td>
<td></td>
<td>The request body (content) received in the response.</td>
</tr>
</tbody>
</table>
#### Error Cases
This method raises an error if the response times out or if the target
server rejects the request. If a web service goes down for some reason, it
can cause scripts that use this method to stop functioning altogether. It
is often a good idea to wrap calls to this method in `pcall` and
gracefully handle failure cases if the required information isn't
available.
#### Limitations
The current limitation for sending and receiving HTTP requests is 500
requests per minute. Requests over this threshold will fail. Additionally,
Roblox domains are excluded. This means that HTTP requests cannot be sent
to any Roblox owned site, such as
[www.roblox.com](https://www.roblox.com).
code_samples:
- sending-an-http-request
parameters:
- name: requestOptions
type: Dictionary
default:
summary: |
A dictionary containing information to be requested from the server
specified.
returns:
- type: Dictionary
summary: |
A dictionary containing response information from the server
specified.
tags:
- Yields
deprecation_message: ''
security: None
thread_safety: Unsafe
events: []
callbacks: []