邮递员身体原始要求Swift Alamofire

时间:2017-09-01 13:56:01

标签: ios swift alamofire

我试图重新创建此邮递员设置,以便在Alamofire中发布。这是我第一次看到一个需要参数和带有Raw Json的主体的API。

我已完成收集和格式化我的数据(使用SwiftyJSON或字典[String : Any] / Parameters在Json中),以满足上述要求。

enter image description here

虽然我确实看到了类似的问题:Postman request to Alamofire request但它没有有效答案。假设我对从各种API发布/获取/ etc数据非常有经验,但我不知道如何传递原始数据,就像上面的照片一样。请在代码中查看我的评论。

以下是我为此请求执行的功能:

/** 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
        }
    }
}
编辑:问题是,我在这里做错了什么? API返回状态码500内部服务器错误。

1 个答案:

答案 0 :(得分:8)

/**
 * @Route("/poznan", name="poznan")
 */
public function poznanPage(Request $request)
{
    return $this->render('default/poznan.html.twig', []);
}

应该是

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