0

I want to perform a post request. My POST data will have several string parameters along with video data. How to accomplish this? What is the best way to upload video along with other parameters like string, array, NSDictionary values?

crypt
  • 449
  • 5
  • 15

1 Answers1

0

You would typically use a HTTP MultiPart Mime POST request message to do this - the message can contain the video and any parameters you want to include.

From your tags I am guessing you want to do this in iOS - if so this answer gives a full example: https://stackoverflow.com/a/24252378/334402

If you want to do it in Android, the following will work (see the title and description parameters being added):

        //Create a new Multipart HTTP request to upload the video
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(serverURL);

        //Create a Multipart entity and add the parts to it
        try {
            Log.d("VideoUploadTask doInBackground","Building the request for file: " + videoPath);
            FileBody filebodyVideo = new FileBody(new File(videoPath));
            StringBody title = new StringBody("Filename:" + videoPath);
            StringBody description = new StringBody("Test Video");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("videoFile", filebodyVideo);
            reqEntity.addPart("title", title);
            reqEntity.addPart("description", description);
            httppost.setEntity(reqEntity);
        } catch (UnsupportedEncodingException e1) {
            //Log the error
            Log.d("VideoUploadTask doInBackground","UnsupportedEncodingException error when setting StringBody for title or description");
            e1.printStackTrace();
            return -1;
        }

        //Send the request to the server
        HttpResponse serverResponse = null;
        try {
            Log.d("VideoUploadTask doInBackground","Sending the Request");
            serverResponse = httpclient.execute( httppost );
        } catch (ClientProtocolException e) {
            //Log the error
            Log.d("VideoUploadTask doInBackground","ClientProtocolException");
            e.printStackTrace();
        } catch (IOException e) {
            //Log the error
            Log.d("VideoUploadTask doInBackground","IOException");
            e.printStackTrace();
        }
Community
  • 1
  • 1
Mick
  • 24,231
  • 1
  • 54
  • 120