My game takes a screenshot of the player’s decor and upload the photo to the server. Using Flash I did it like this:
var form:Multipart = new Multipart(upload_url);
form.addFile("file1", PNGEncoder.encode(bitmapData), "application/octet-stream", "test.jpg");
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(form.request);
Now I’m trying to do it like this - but the server does not accept photo data:
-- here i'm using screenshot extension
local screenshot_png, w, h = screenshot.png()
http.request(upload_url, "POST", function(self, _, response)
pprint(response)
end, {["Content-Type"] = "application/octet-stream"}, "file1="..screenshot_png)
I tested this from a computer.
I am trying to upload a photo to the VK.com server. Here is what is written in their documentation:
Send files to the upload_url address forming a POST request with file1 - file5 fields. These fields should contain images in multipart/form-data format.
I tried using a module to form a request, but this still does not work for me
Okay, I finally figured out how to form the post body . And even progress has been outlined - instead of an empty answer, I began to receive a “bad request”
-- create 100x100 red pixels
local w = 100
local h = 100
local pixels = string.rep(string.char(255,0,0), w * h)
-- encode
local contents = png.encode_rgb(pixels, w, h)
local filename = "file1"
local boundary = "WebKitFormBoundaryePkpFF7tjBAqx29L"
local body = "--"..boundary..
"\rContent-Disposition: form-data; "..
'name="'..filename..'"; filename="'..filename..'"'..
"\rContent-Type: image/png"..
"\r\n"..contents..
"\r--"..boundary.."--";
local headers = {
["Content-Type"] = "multipart/form-data; boundary="..boundary,
["Content-Length"] = #body
}
http.request(jd1.upload_url, "POST", function(self, _, response)
pprint(response)
end, headers, body)