ios – Swift Alamofire: why after I add multipart/form-data I all the time get “Request Physique None”

0
205
ios – Swift Alamofire: why after I add multipart/form-data I all the time get “Request Physique None”


I am attempting to add a multipart/form-data request utilizing Alamofire in Swift to my backend working on localhost:8080. Once I ship the request, the server response is a 403 error, and the request physique all the time exhibits as None. Nevertheless, after I carry out the identical request utilizing Postman, every part works high-quality, and I obtain the anticipated 200 OK response.
Here is the code I am utilizing with Alamofire:

    func createLotto(newLotto: NewLottoData, token: String) {
        print(newLotto)
        
        AF.add(multipartFormData: { multipartFormData in
     
            multipartFormData.append(newLotto.nomeLotto.information(utilizing: .utf8)!, withName: "nomeLotto")
            multipartFormData.append(newLotto.budgetLotto.information(utilizing: .utf8)!, withName: "budgetLotto")
            multipartFormData.append(newLotto.descrizioneLotto.information(utilizing: .utf8)!, withName: "descrizioneLotto")
            multipartFormData.append(newLotto.emailUtente.information(utilizing: .utf8)!, withName: "emailUtente")
            
      
            let prodottiListData = attempt! JSONEncoder().encode(newLotto.prodottiList)
            let prodottiListJSONString = String(information: prodottiListData, encoding: .utf8)!
            print(prodottiListJSONString)
            multipartFormData.append(prodottiListJSONString.information(utilizing: .utf8)!, withName: "prodottiList")
            

            multipartFormData.append(newLotto.imgCopertina, withName: "imgCopertina", fileName: "copertina.jpg", mimeType: "picture/jpeg")
            
         
            for (index, picture) in newLotto.prodottiImmagini.enumerated() {
                multipartFormData.append(picture, withName: "prodottiImmagini[(index)]", fileName: "image_(index).jpg", mimeType: "picture/jpeg")
            }
        },
        to: "http://localhost:8080/lotti/createLotto",
        methodology: .put up,
        headers: [
            "Authorization": "Bearer (token)"
        ]).responseDecodable(of: GenericResponse.self) { response in
            debugPrint(response)
        }
    }





import UIKit

struct NewLottoData: Encodable {
    let nomeLotto: String
    let budgetLotto: String
    let descrizioneLotto: String
    let emailUtente: String
    let prodottiList: [NewProdotto]
    let imgCopertina: Information
    let prodottiImmagini: [Data]
}

In my debug output, I all the time see [Body]: None, which means that the request physique is not being despatched accurately. I’ve confirmed that each one information (strings and pictures) exist earlier than importing, and I’ve tried setting numerous Content material-Sort headers, however nothing appears to work.

[enter image description here](https://i.sstatic.web/KxxKnGyI.png)

Right here’s some extra info:

It’s not a problem with the token. I can verify that it’s legitimate.
I’ve printed out all the info earlier than sending the request, and every part seems appropriate.
The pictures are initially of kind UIImage, and I can show them within the app earlier than sending the request. I’ve transformed the pictures utilizing .pngData()! to Information kind, and that appears to work high-quality.

Alamofire model 5.9.1

Any assist could be significantly appreciated!

I attempted importing the shape information utilizing Alamofire with the supplied parameters, together with textual content fields and pictures, anticipating the request physique to be correctly despatched, identical to it really works in Postman. I used to be anticipating a 200 standing code response from the server and the info to be processed as it’s in Postman.

Nevertheless, what I acquired as a substitute was a 403 Forbidden error, and the debug output signifies that the request physique is empty (None)

LEAVE A REPLY

Please enter your comment!
Please enter your name here