Dzeremix
Dzeremix

Reputation: 478

Sending video on server by multipart form data

I have a problem with pushing video on the server. My API has two parameters in body: details and file and I must authorize it by token in header.

At the beginning my file was prepared and exported to the URL that I am pushing to method. Next with details (string value) I am trying to prepare it to send on the server.

After I run my app I have got this error:

Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}

Server in respond returns all data about pushed video in JSON

I used this tutorial to try make some first step: https://mindfiremobile.wordpress.com/2014/01/27/upload-video-to-server-using-multiparts/

It my first steps with pushing video on the server and I will be glad for help.

func postDetailsWithVideo(details: String, file: NSURL) {
    let url = serverURL.URLByAppendingPathComponent("api/details")

    let videoData = NSData.init(contentsOfURL: file)

    let kBoundary = "---------------------------14737809831466499882746641449"
    let kStartTag = "--%@\r\n"
    let kEndTag = "\r\n"
    let kContent = "Content-Disposition: form-data; name=\"%@\"\r\n\r\n"

    let body = NSMutableData()

    //details data
    body.appendData(String(format: kStartTag, kBoundary).dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(String(format: kContent, "storyDetails").dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(details.dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(String(format: kEndTag).dataUsingEncoding(NSUTF8StringEncoding)!)

    //Video data
    body.appendData(String(format: kStartTag, kBoundary).dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("Content-Disposition: form-data; file=\"flv\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("Content-Type: application/octet-stream\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(NSData(data: videoData!))
    body.appendData(String(format: kEndTag).dataUsingEncoding(NSUTF8StringEncoding)!)

    // close form
    body.appendData("--\(kBoundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

    let contentType = "multipart/form-data; boundary=\(kBoundary)"

    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.setValue("Bearer \(DataManager().getAccessToken())", forHTTPHeaderField: "Authorization")
    request.setValue(contentType, forHTTPHeaderField: "Content-Type")
    request.HTTPBody = body

    print(body)

    postDataOnTheServer(request, apiMethod: "api/details")

}

func postDataOnTheServer(request: NSMutableURLRequest, apiMethod: NSString) {
        let currentRequest = request

        let task = NSURLSession.sharedSession().dataTaskWithRequest(currentRequest){ data, response, error in
            if error != nil{
                self.delegate?.errorOccured(apiMethod, error: error!)
                return
            }

            do {
                let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]

                self.delegate?.serverResponseFromAPIMethod(apiMethod, result: result!)

            } catch {
                print("response - some error")
                print(error) //do something with me
            }
        }

        task.resume()
    }

Upvotes: 1

Views: 2669

Answers (1)

Reckoner
Reckoner

Reputation: 1041

Its a server issue.Ask your backend not to echo anything and you will get the response.

Upvotes: 1

Related Questions