如何在Alamofire正文和参数中创建请求

时间:2019-07-28 21:01:57

标签: ios xcode alamofire

我是新手程序员,我对搜索有疑问。 我需要创建一个请求主体+参数,我发现了类似 Postman Body Raw Request To Swift Alamofire 但是我仍然不知道将身体放在哪里以及参数在哪里。 这是我的要求:

let usersContactInformation: UsersContactInformationBody =  LoginGsonService.init().getUserCreateBody(username: email, password: password, firstName: firstName, secondName: secondName, numberPhone: numberPhone, imei: imei)

    func parametersAccess_token(access_token: String) -> Parameters?  {
            return  ["access_token" : access_token]
        }

            AF.request(MainApi.userCreate,  method: .post, parameters: parametersAccess_token, encoding:
                JSONEncoding.default, headers: HTTPHeadersApi.headersLoginPassword(username: email, password: password)).responseJSON {
                    response in
                        switch response.result {
                        case .success:
                            do {
                                   print(response)
                            }
                            catch {
                                print(error)
                                self.view.error(message: "\(error)")
                            }
                            break
                        case .failure(let error):
                            print(error)
                            self.view.error(message: "\(error)")
                        }
                }

1 个答案:

答案 0 :(得分:0)

这是带有http标头的演示帖子请求。实际请求取决于您的服务器实现。

let user = "user"
let password = "password"

var headers: HTTPHeaders = [:]

if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
    headers[authorizationHeader.key] = authorizationHeader.value
}
headers[access_token] = access_token
Alamofire.request("\(MainApi.userCreate)/user/password", headers: headers)
        .responseJSON { response in
            debugPrint(response)
        }