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

PostAsync

The PostAsync function sends an HTTP POST request, blocking the current thread until a response is received. Certain HTTP response codes (like 404 or 403) will raise errors.

A useful endpoint that can help you debug POST requests is https://httpbin.org/post. It provides a JSON response with information about a POST request, such as headers and URL arguments.

See here for a detailed guide on sending and ``HttpService/PostAsync|retrieving` data via HTTP requests.

##Function arguments

  • Compression uses gzip. If you set the compress parameter to true, your data will be gzipped.
  • PostAsync sends the data in the data argument to the address given by url. The content_type argument modifies the value in the Content-Type header sent with the request, which tells the server the format of the data passed in data.

##Requirements and limitations

  • The protocol (“http://” or “https://”) must be present at the beginning of url, or PostAsync will throw a “trust check failed” error.
  • Posting 1024KB or more of data will result in an error. (1024KB: A string with a length of 1024^2 characters)
  • See also the restrictions documented at HttpService

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

Tells the server the format of the data passed in data

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)