PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

PostAsync

The PostAsync function sends an HTTP POST request. It functions similarly to 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 HttpService/RequestAsync|RequestAsync should to be used in most cases. For a detailed guide on sending and retrieving data via HTTP requests, see the Sending HTTP Requests article.

When true, the compress parameter controls whether large request bodies will be compressed using gzip.

Parameters

Name Type Default Description

url

The destination address for the data

The data being sent

ApplicationJson

Modifies the value in the Content-Type header sent with the request

false

Determines whether the data is compressed (gzipped) when sent

Used to specify some HTTP request headers

Returns

Return Type Summary

The HTTP response sent back indicating the request result


Code Samples


New Pastebin Post

Pastebin.com is a website that allows users to paste text (usually source code) for others to view publicly. This code sample uses HttpService PostAsync and the pastebin web API to automatically create a new public paste on the website. Since pastebin’s API is designed to take data in as a URL encoded string, the code uses a for-loop to turn the dataFields table into a URL encoded string, such as hello=world&foo=bar. This is used as the HTTP POST data.

Test this code by first going to pastebin.com/api#1 and getting an API key (you’ll need a pastebin account to do this). Then, paste your unique developer API key into the field api_dev_key in the code sample’s dataFields table. Fill in any other information about the post you want to make, then run this code in a Script (not a LocalScript). If all goes well, you’ll get a URL to your new paste in the Output window (or some error string from pastebin).

local HttpService = game:GetService("HttpService")

local URL_PASTEBIN_NEW_PASTE = "https://pastebin.com/api/api_post.php"
local dataFields = {
	-- Pastebin API developer key from
	-- https://pastebin.com/api#1
	["api_dev_key"] = "FILL THIS WITH YOUR API DEVELOPER KEY";    
	
	["api_option"] = "paste";                                -- keep as "paste"
	["api_paste_name"] = "HttpService:PostAsync";            -- paste name
	["api_paste_code"] = "Hello, world";                     -- paste content
	["api_paste_format"] = "text";                            -- paste format
	["api_paste_expire_date"] = "10M";                       -- expire date        
	["api_paste_private"] = "0";                             -- 0=public, 1=unlisted, 2=private
	["api_user_key"] = "";                                   -- user key, if blank post as guest
}

-- The pastebin API uses a URL encoded string for post data
-- Other APIs might use JSON, XML or some other format
local data = ""
for k, v in pairs(dataFields) do
	data = data .. ("&%s=%s"):format(
		HttpService:UrlEncode(k),
		HttpService:UrlEncode(v)
	)
end
data = data:sub(2) -- Remove the first &

-- Here's the data we're sending
print(data)

-- Make the request
local response = HttpService:PostAsync(URL_PASTEBIN_NEW_PASTE, data, Enum.HttpContentType.ApplicationUrlEncoded, false)
-- The response will be the URL to the new paste (or an error string if something was wrong)
print(response)