0

I would like to upload an image to a server by using NSURLSessionUploadTask, which I have already accomplished by using a custom delegate

let task =  NSURLSession(
    configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
    delegate: self,
    delegateQueue: NSOperationQueue.mainQueue()
).uploadTaskWithRequest(request, fromData: data)

task!.resume()

But I am unable to get the server's response. Using task.response returns the error code and other header information, but does not return the actual html shown on the page.

I can't create the url task with a completion handler, because I need to get the progress of the upload.

How can I get the NSData from a page response using NSURLSessionUploadTask?

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
  • Did you implement NSURLSessionTaskDelegate in your class? The delegate methods should be invoked to indicate the progress of upload and also after completion.. – Subbu Jul 06 '15 at 23:55
  • @Subbu The problem is that I'm not sure how to get the server's response. I'm able to get the progress, and I'm able to see when the upload is completed, but I'm unable to see what the server's response html is – Jojodmo Jul 07 '15 at 01:31
  • hmm.. may be task.response.data has the info? did you want to convert the data to nsstring and print to see if that is the html.. something like var datastring = NSString(data:task.response.data, encoding:NSUTF8StringEncoding) as String? – Subbu Jul 07 '15 at 01:40
  • @Subbu Sadly, theres no `task.response.data` – Jojodmo Jul 07 '15 at 02:02
  • My apologies.. Just trying to help, can you try gathering the data in "didReceiveData" .. [you could have a global NSData instance and append the received data ] .. In 'didCompleteWithError' you can convert the data to string... – Subbu Jul 07 '15 at 03:00
  • @Subbu Actually, I was wrong. There is a `didReceiveData` method, and it works. Could you please post that in an answer so I can accept it? Thank you for your help! – Jojodmo Jul 08 '15 at 20:46
  • Yay!! i am so glad it worked for you.. – Subbu Jul 08 '15 at 20:49

1 Answers1

1

You can gather the data via didReceiveData, and could have a global NSData instance and append the received data.

Then, in didCompleteWithError, you can convert the received data to a string to get the response HTML.

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
Subbu
  • 2,138
  • 14
  • 20