iOS: How to get a specific string from a Data containing Strings and an image?

382 Views Asked by At

I have that code that add values to body:

let body = NSMutableData()
let mimetype = "image/jpg"

//define the data post parameter

body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"eventId\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\(eventId)\r\n".data(using: String.Encoding.utf8)!)

body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"contactId\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\(contactId)\r\n".data(using: String.Encoding.utf8)!)

body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"type\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\(type)\r\n".data(using: String.Encoding.utf8)!)

body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"file\"; filename=\"\(fileName)\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(image_data!)
body.append("\r\n".data(using: String.Encoding.utf8)!)

body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)

request.httpBody = body as Data

How to convert it in string? I would like to get the boundary, but impossible to do it.

When I do that, it makes me nil: let test = String(data: body as Data, encoding: .utf8)

1

There are 1 best solutions below

0
Leo Dabus On

If you need your boundary you should create its header and footer data in a separate object from your image data:

let boundary = UUID().uuidString
let eventId = "your event ID"
let contactId = "your contact ID"
let type = "your type string"
let mimetype = "image/jpg"
let fileName = "the file name"
let imageData = Data() // your image data
let boundaryHeader = Data("""
--\(boundary)\r\n
Content-Disposition:form-data; name=\"eventId\"\r\n\r\n
\(eventId)\r\n
--\(boundary)\r\n
Content-Disposition:form-data; name=\"contactId\"\r\n\r\n
\(contactId)\r\n
--\(boundary)\r\n
Content-Disposition:form-data; name=\"type\"\r\n\r\n
\(type)\r\n
--\(boundary)\r\n
Content-Disposition:form-data; name=\"file\"; filename=\"\(fileName)\"\r\n
Content-Type: \(mimetype)\r\n\r\n
""".utf8)

let boundaryFooter = Data("""
\r\n
--\(boundary)--\r\n
""".utf8)

let body = boundaryHeader + imageData + boundaryFooter

var request = URLRequest(url: URL(string: "http://www.example.com/whatever")!)
request.httpBody = body

print(String(data: boundaryHeader, encoding: .utf8) ?? "nil")

This will print:

Content-Disposition:form-data; name="contactId"

your contact ID

--2583374D-68AF-4EE1-96A5-740CCA17C51D

Content-Disposition:form-data; name="type"

your type string

--2583374D-68AF-4EE1-96A5-740CCA17C51D

Content-Disposition:form-data; name="file"; filename="the file name"

Content-Type: image/jpg