Hi Guys I have a POST Request And I'm Usualy facing a simple form of data in body but not like this shape I want to know how can I send this form of body in this shape
the form of body I'm trying to insert in postman
from the this image I'am getting Color from a textfield and Black from another textfield ex: let key = colorTextField.text, let value = "black" this is my first time to see a shape of body like this can you help me please to make me know how can I send body like this with my request .... I'm not using any type of pods to make my request and this is the request I'm trying to send the body with ... :
the only thing I want is know how can I send a body like this in the Image
which the key is: properties[] and value: "{ "key" : "Color" , "values" : ["White" ,"Black" ] }"
please open also the image to know what I mean
let url = URL(string: "http://18.224.184.27:7000/api/v1/profile/store/add-categories")!
let body = "the body aim trying to send"
var request = URLRequest(url: url)
request.httpMethod = "POST"
let token = User.fetch()?["token"] as? String
request.setValue(token , forHTTPHeaderField: "x-auth-token")
let boundary = "Boundary-(NSUUID().uuidString)"
request.setValue("multipart/form-data; boundary=(boundary)", forHTTPHeaderField: "Content-Type")
var imageData = Data()
// if picture has been selected, compress the picture before sending to the server
if isPictureSelected {
imageData = avaImageView.image!.jpegData(compressionQuality: 0.5)!
}
//Building The Full Body (text + file parameters)
//filename = any file name i want to rename which have the image but i set the NSUUID().uuidString to be randomly generataed
request.httpBody = Helper().body(with: body, filename: "(NSUUID().uuidString).jpg", filePathKey: "file", imageDataKey: imageData, boundary: boundary) as Data
// STEP 2. Execute created above request
URLSession.shared.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
// access helper class
let helper = Helper()
// error
if error != nil {
helper.showAlert(title: "Server Error", message: error!.localizedDescription, in: self)
return
}
// fetch JSON if no error
do {
// save mode of casting data
guard let data = data else {
helper.showAlert(title: "Data Error", message: error!.localizedDescription, in: self)
return
}
//fetching all JSON received from the server
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary
// save mode of casting JSON
guard let parsedJSON = json else {
print("Parsing Error")
return
}
print(currentUser as Any)
// STEP 4. Create Scenarious
// Successfully Registered In
if (parsedJSON["statusCode"] != nil) {
let message = parsedJSON["message"] as! String
appDelegate.infoView(message: message, color: colorLightGreen)
Profile.save(json: parsedJSON)
// sending notification to other vcs
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateUser"), object: nil)
Helper().instantiateViewController(identifier: "TabBar", animated: true, by: self, completion: nil)
// Some error occured related to the entered data, like: wrong password, wrong email, etc
} else {
let response = parsedJSON["error"] as? [String:AnyObject]
let message = response?["message"] as? String
helper.showAlert(title: "Error", message: message!, in: self)
appDelegate.infoView(message: message!, color: colorSmoothRed)
}
// error while fetching JSON
} catch {
helper.showAlert(title: "JSON Error", message: error.localizedDescription, in: self)
}
}
}.resume()
}
question from:
https://stackoverflow.com/questions/65853176/send-this-form-of-body-with-post-request-using-swift 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…