Http Post Request wont allow Content Length Header

  1. What do you want to achieve? Keep it simple and clear!
    I want to send an http post request using requestasync
  2. What is the issue? Include screenshots / videos if possible!
    When I try to send the post headers an error pops up saying: Header Must Be Table
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    A few things to keep in mind when reading my script, the website I’m trying to access is weird as it only accepts JSON or XML (XML Is clearly not an option, but JSON encodes properly when printed) The error is caused by using httpService:JsonEncode().
    The Headers Following Headers are all required otherwise the request will be denied.
    [“Content-Length”] - the number of characters in the body. (This one errors when I remove the JSON encode, also need help with finding the length of a table.)
    [“Date”] - the date in a format which I have successfully done
    [“Content-Type”] - the format in which the data is being sent
    My script so far:
local dateformat= "%b, %d %m %Y %X UTC"
local date = os.date(dateformat)
local post = { -- table here
	Method = "POST",
	Url = "https://my.website.com/",
	Body = HttpService:JSONEncode({
			["PartitionKey"] = Id,
			["RowKey"] = newplayer,
			["Money"] = 100,
			["OWNSGAMEPASS"] = true
		}),
}
post.Headers = {}
post.Headers["Date"] = date 
post.Headers["Content-Type"] = "application/json"
local length = string.len(HttpService:JSONEncode(post.Body))
print(length)
post.Headers["Content-Length"] = length
local postresponse = HttpService:RequestAsync(post)

part of the script inside of the “Post” table is based off of the HttpService:RequestAsync() article in the dev api.
The part Outside the post table is another way to do it based off of this article that announced the feature Requestasync() and how it supported all methods and content types.
Essentially I need to be able to use “Content Length”

1 Like

You can’t.

However, some headers cannot be specified. For example, Content-Length is determined from the request body.

From HttpService | Documentation - Roblox Creator Hub

1 Like

You don’t need this line anymore (RE: my last reply), but it’s also a bug. post.Body is already a JSON string. You are trying to encode it twice, which will not give the result you expect.

1 Like