0

I'm new In Swift anyone help me.

I want to pass array of object to Alamofire and I don't know how to do that

Here is the parameter that required:

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZWU4OGZiNzhiYTBkMjMyZDFmYWZkMzgiLCJpYXQiOjE1OTIyOTk2Njh9.AVuxiTZy10fV2ZMZcT-oHXSg6PdK3tfE",
    "zipCodes": [
        {
            "zip_code": "55001",
            "city": "Afton",
            "state": "MN",
            "county": "Washington"
        }
    ]
}

And I do that

 let parameters : [String : String] = ["token" : retrivedToken, "zipCodes" : [{
            "zip_code": "55001",
            "city": "Afton",
            "state": "MN",
            "county": "Washington"
            }]
        ]

2 Answers 2

2

Simply just use your parameter to your request.

func sendRequestRequest() {
// JSON Body
let parameters: [String : Any] = [
    "token": retrivedToken,
    "zipCodes": [
        "county": "Washington",
        "state": "MN",
        "zip_code": "55001",
        "city": "Afton"
    ]
]

// Fetch Request
Alamofire.request("your API url", method: .post, parameters: parameters, encoding: JSONEncoding.default)
    .validate(statusCode: 200..<300)
    .responseJSON { response in
        if (response.result.error == nil) {
            print("HTTP Response Body: \(response.data)")
        }
        else {
            print("HTTP Request failed: \(response.result.error)")
        }
    }

}

Dont forget, if you use Alamofire v5, use AF.request instead of Alamofire.request

Sign up to request clarification or add additional context in comments.

1 Comment

From Documentations: "There are additional methods that allow you to make requests using Parameters dictionaries and ParameterEncoding types. This API is no longer recommended and will eventually be deprecated and removed from Alamofire." It will be removed so how it will be done after that?
0
let bodyParams: [String : Any] = [
            "token": "\(retrivedToken)",
            "zipCodes":[
                "county": "Washington",
                "state": "MN",
                "zip_code": "55001",
                "city": "Afton"
            ]
        ]
    }

let urlString = "abc.com"

Alamofire.request(urlString, method: .post, parameters: bodyParams ,encoding: JSONEncoding.default, headers: nil).responseJSON {  
response in
  switch response.result {
                case .success:
                    print(response)
                    break
                case .failure(let error):

                 print(error)
      }
}

2 Comments

What's the purpose of this answer? This is the same like what i wrote earlier.
From Documentations: "There are additional methods that allow you to make requests using Parameters dictionaries and ParameterEncoding types. This API is no longer recommended and will eventually be deprecated and removed from Alamofire." It will be removed so how it will be done after that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.