Postman Body Raw Request To Swift Alamofire

爷,独闯天下 提交于 2019-12-19 04:25:14

问题


I'm trying to re-create this Postman settings for posting in Alamofire. This is my first time to see an API that requires both Parameters and a body with Raw Json.

I'm done with gathering and formatting my data (either in Json using SwiftyJSON or Dictionary [String : Any] / Parameters) for the said requirement.

While I did see a similar question to this: Postman request to Alamofire request but it doesn't have a valid answer. Assume that I'm quite experienced with posting/getting/etc data from various API but I just don't know how to pass raw data just like in the photo above. Please check out my comments too in the code.

Here's what I'm doing with my function for this request:

/** Apply to job with Shift.
 *  This service function creates a json data for applying.
 */

func someFuncService(_ job: Job, daySchedules: [(Int, String, Schedule)], withBlock completion: @escaping JobServiceCommonCallBack) {
    AuthService.someFunc { (currentCustomer, accessToken) in
        guard let lalala = currentCustomer?.id,
            let accessT = accessToken else {
                completion(LalaErrors.currentCustomerError)
                return
        }

        guard let jobId = job.id else {
            completion(LalaErrors.modelError)
            return
        }


        let coreService = LalaCoreService()

        let applicantEndpoint = LalaCoreService.Endpoint.Applicant

        let parameters = [
            "param1" : customerId,
            "param2" : jobId,
            "accessToken" : accessToken,
            "shift" : self.generateDataFromDaySchedules(daySchedules) // this returns [String : Any], can be printed into Json using JSON(x)
            ] as Parameters

        GPLog(classSender: self, log: "FINAL PARAMETER: \(parameters)")

        coreService.request = Alamofire.request(
            applicantEndpoint,
            method: .post,
            parameters: parameters,
            encoding: URLEncoding.default, // I already have tried .httpbody too.
            headers: nil
        )

        coreService.request {
            (response, result) in

            if let error = result?.error {
                if response!.statusCode == 500 {
                    completion(GPKitError.newError(description: "Failed to apply. Please contact the admin."))
                    return
                }

                completion(error)
                return
            }

            // Success
            completion(nil)
            return
        }
    }
}

EDIT: So the question is, what I'm doing wrong here? API returns me status code 500 internal server error.


回答1:


  coreService.request = Alamofire.request(
        applicantEndpoint,
        method: .post,
        parameters: parameters,
        encoding: URLEncoding.default, // I already have tried .httpbody too.
        headers: nil
    )

should be

  coreService.request = Alamofire.request(
            applicantEndpoint + accessToken,
            method: .post,
            parameters: parameters,
            encoding: JSONEncoding.default, 
            headers: nil
        )


来源:https://stackoverflow.com/questions/46001798/postman-body-raw-request-to-swift-alamofire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!