Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Class: HTTPRequestBuilder

smokingplaya edited this page May 24, 2024 · 13 revisions

The HTTPRequestBuilder class was created as an abstraction over sending HTTP requests.
It simplifies sending HTTP requests, makes it easier to understand.

Methods

new (constructor)

@argument URL (string) - The URL link that will be used to send the request.
@argument Method (enum METHODS) - The method through which the request will be sent.

Example

local req = HTTPRequestBuilder:new("https://google.com", METHODS.GET)

SetContentType?

@description Sets content type of request

@argument Type (enum MIME_TYPES) - MIME type of content

Example

req:SetContentType(MIME_TYPES.JSON)

SetHeader?

@description Sets header value of request

@argument HeaderName (string) - Name of header @argument HeaderValue (string) - Value of header

Example

req:SetHeader("Accept", MIME_TYPES.JSON)

SetCallback

@description Sets callback of request

@argument Callback (function with arguments: object HTTPResponse) - Function, that calls when we got response of server

Example

req:SetCallback(function(res)
  dump("res", res)
end)

SetBody?

@description Sets body of request

@argument Body (any) - Body

Example

req:SetBody({
   example_key: "Hello, world!"
}) -- It will be converted to a string when sent

SetHTTP?

@description Sets function, that will be send HTTP request @default HTTP

@argument HTTPFunction (function) - function, that will be send HTTP request

Example

require("chttp") -- https://github.com/timschumi/gmod-chttp

req:SetHTTP(CHTTP)

Send

@description Sends HTTP request

Example

HTTPRequestBuilder:new("https://google.com/", METHODS.GET)
     :SetCallback(function(res)
         dump("response", res)
     end)
     :Send()
Clone this wiki locally