I dont understand :RequestAsync

im learning httpservice but the devhub doesnt really explain the RequestAsync function that well, can someone please explain it to me

RequestAsync. I think it’s pretty thorough if you ask me.

RequestAsync is a function that allows you to perform HTTP requests via HttpService. For this you should have a fundamental understanding of HTTP methods or at least the ones you’ll be using most often (GET and POST). The two examples I provided have shorthands, GetAsync and PostAsync. These function similarly to RequestAsync but are scoped to their specific request type and are not extensive in terms of what you can give and get from an HTTP request.

Ideally for all HttpService work you should tap into RequestAsync. I’m not a web developer nor one with a strong grasp on HTTP so details about performing requests and what it means would be best answered by someone familiar with that, but at least from the Luau side hopefully the above explanation should help explain it a bit.

In shorter terms: it just allows you to perform different types of HTTP methods. If RequestAsync didn’t exist, then there’d have to be different methods to perform every type of Http request there is (e.g. HttpService:PatchAsync, HttpService:DeleteAsync, HttpService:PutAsync, etc).

In even shorter terms: RequestAsync works as a one-size-fits-all method for HTTP requests.

3 Likes

The parameters of RequestAsync are broken down into four groups: url, method, headers, and body.

The URL is the full URI of the website path you are requesting.

The method can be one of the following:

  • GET - For fetching information (typically)
  • POST - For submitting information (typically)
  • PUT - For updating or replacing information (typically)
  • PATCH - For updating or patching/fixing information (typically)
  • DELETE - For deleting information (typically)

Headers are specific bits of information (in the form of a dictionary) that are provided along with the request body (such as Content-Type to specify what format the body is in, etc.)

The body is the actual data that is being sent by the request. If you understand NodeJS & ExpressJS, this should give you an understanding of how the body is used.

const app = require('express')()

// configure middleware so the body is actually passed through

app.post('/endpoint', (req, res) => {
    const body = req.body
    res.send(req.body.SomeDataInTheBody)
})

app.listen(8080)
3 Likes