I’ve tried a bunch of things to upload a video to parse and I’m really close. I have the following information that would help us figure this out: Parse REST API, plus a file that uses the Multiform Data ( http://developer.coronalabs.com/code/how-upload-image-server-multipartform-data). First, I have the following code which ends up giving me a ‘Bad Gateway - Error 502 - The server returned an invalid or incomplete response’ in the Upload response.
local function onComplete(event)
if event.completed then
local videoURL = event.url
local sourcePath = string.sub(videoURL,8) – prints out path. ex… “/storage/emulated/0/DCIM/Camera/20140706_175957.mp4”
local videoName = string.sub(videoURL, 40) – prints out video title and extension. ex…“20140706_175957.mp4”
local videoFile = string.sub(videoURL, 40, -5) – prints out video title. ex… “20140706_175957”
multipart:addHeader(“X-Parse-Application-Id”, myData.appId)
multipart:addHeader(“X-Parse-REST-API-Key”, myData.apiKey)
– multipart:addHeader(“Content-Type”, “video/mp4”) – Maybe Needed
multipart:addFile(videoFile, sourcePath, “video/mp4”, videoName)
local params = {}
params.body = multipart:getBody() – Must call getBody() first!
params.headers = multipart:getHeaders() – Headers not valid until getBody() is called.
local function onFileUploaded( event )
if not event.error then
if event.phase == “ended” then
local resulted = event.response – Currently Get Bad Gateway 502 Error Code
local statused = event.status – Prints out 502
end
else
end
end
network.request( “https://api.parse.com/1/files/”…videoName, “POST”, onFileUploaded, params)
else
end
end
if media.hasSource( media.Camera ) then
media.captureVideo( { listener = onComplete, preferredMaxDuration = 3, } )
else
native.showAlert( “Sorry”, “This device does not have a camera.”, { “OK” } )
end
Uploading Files
To upload a file to Parse, send a POST request to the files URL, postfixed with the name of the file. The request must contain the Content-Type header associated with the file. Keep in mind that files are limited to 10 megabytes. Here’s a simple example that’ll create a file named hello.txt containing a string:
curl -X POST \
-H “X-Parse-Application-Id: L39R3jVAaGSvjmLtqntxbejApgJjhe15afK5Z5bI” \
-H “X-Parse-REST-API-Key: QwOBH0wwKEYX2edHIgdYhbPUD6nk9xDG0otFQDHy” \
-H “Content-Type: text/plain” \
-d ‘Hello, World!’ \
https://api.parse.com/1/files/hello.txt
Show examples for: python curl Use keys for: Gotcha You R It
When the file upload is successful, the HTTP response is a 201 Created and the Location header which contains the URL for the file:
Status: 201 Created
The response body is a JSON object containing the name of the file, which is the original file name prefixed with a unique identifier in order to prevent name collisions. This means you can save files with the same name, and the files will not overwrite one another.
{
“name”: “db295fb2-8a8b-49f3-aad3-dd911142f64f-hello.txt”
}
To upload an image, the syntax is a little bit different. Here’s an example that will upload the image myPicture.jpg from the current directory.
curl -X POST \
-H “X-Parse-Application-Id: L39R3jVAaGSvjmLtqntxbejApgJjhe15afK5Z5bI” \
-H “X-Parse-REST-API-Key: QwOBH0wwKEYX2edHIgdYhbPUD6nk9xDG0otFQDHy” \
-H “Content-Type: image/jpeg” \
--data-binary ‘@myPicture.jpg’ \
https://api.parse.com/1/files/pic.jpg
Show examples for: python curl Use keys for: Gotcha You R It