-1

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)

Nimantha
  • 6,405
  • 6
  • 28
  • 69
ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • Use Data instead, `var body = Data()` – Joakim Danielson Apr 15 '20 at 09:26
  • @JoakimDanielson I did it, still the same. May be it is because of the big image I encoded? – ΩlostA Apr 15 '20 at 09:41
  • The issue here is that the image data can’t be converted to string. What is your goal? – Leo Dabus Apr 15 '20 at 09:56
  • @LeoDabus My goal is just to retrieve the boundary String in this data. I added a security pinning, and I don't want to break all the code I made if it is possible. If I could get the boundary value in the data, it should be very helpful. The other case, I have to redo all I have done – ΩlostA Apr 15 '20 at 09:58
  • "How to convert it in string? I would like to get the boundary, but impossible to do it." Insn't the boundary in the headers? Else, you can't convert that into String using utf8 because you'll often have an data that can't be converted as such (like an image). But if it's at the start, you can search into data "--" the first occurence, then look for the first occurence of "\r\n", and guess the boundary. – Larme Apr 15 '20 at 10:01
  • 1
    Like the others said, you can't convert the image data to a utf8 string. If it's just the boundary you want then you can convert the data to an ascii String. As long as the boundary only contains ascii characters that will work. However, it doesn't feel to me like a robust or safe way of doing it, so I wouldn't recommend it. But I'm a bit confused about the request - you're adding the boundary yourself through string interpolation so why don't you already have a reference to it? – Pete Morris Apr 15 '20 at 10:16

1 Answers1

1

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

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571