Alamofire错误:数据无法读取,因为它的格式不正确

时间:2015-12-13 15:03:52

标签: php swift request alamofire

尝试通过Alamofire通过POST请求将参数发送到我的服务器时收到错误。

    // Create payload to be submitted to the server
    let userReference = appDelegate.userReference
    let parameters : [String : AnyObject] = [
        "userReference": userReference,
        "pushToken": pushToken,
        "installationReference": ""
    ]

    Server().sendPOSTRequestToServer(endpoint: "devices", parameters: parameters) { (response) -> Void in
        let responseCode = response["code"]
        switch(responseCode) {
        case 201:
            completionHandler(response: response)
            break
        default:
            break
        }
    }
}

func sendPOSTRequestToServer(endpoint endpoint: String, parameters: [String:AnyObject], completionHandler: (response: JSON) -> Void) {
    Alamofire.request(.POST, self.requestUrl + endpoint, parameters: parameters, encoding: .JSON).responseJSON { (response) -> Void in
        switch response.result {
        case .Success:
            completionHandler(response: JSON(response.result.value!))
        case .Failure(let error):
            print("POST REQUEST ERROR - \(endpoint) - \(error.localizedDescription)")
            print("=== REQUEST INFORMATION ===")
            print("Status Code: \(response.response!.statusCode)")
            print("Request URL: \(response.request!.URLString)")
            print("Request Payload: \(parameters)")
            print("===")
        }
    }
}

我收到的错误是:

> POST REQUEST ERROR - devices - The data couldn’t be read because it
> isn’t in the correct format.
> === REQUEST INFORMATION === Status Code: 200 Request URL: http://SERVERIP/v1/devices Request Payload: ["pushToken":
> PUSHTOKEN,
> "userReference": res_hq9gpcgap09joe, "installationReference": ABC]
> ===

我猜错了格式化参数数组。这个错误来自哪里?

服务器代码:

// Store new Device
    public function store(Request $request) {
        // Get payload from request
        $bodyContent = json_decode($request->getContent(), true);
        $installationReference = $bodyContent["installationReference"];
        $devicesFound = DeviceHelper::searchForExistingDevice($installationReference);

        if ($devicesFound == 0) {
            // If no device is found, create unique id for device as installation id and execute command to register device.
            $newInstallationReference = "ins_" . CustomHelper::createUniqueId(14);
            $registerDeviceCommand = new RegisterDeviceCommand($bodyContent, $newInstallationReference);
            $this->commandBus->execute($registerDeviceCommand);
            $this->respondCreated("Device was created.");

            return json_encode(array("code" => $this->statusCode, "installationReference" => $newInstallationReference), JSON_NUMERIC_CHECK);
        } else {
            $this->setStatusCode(404)->respondWithError("Device already exists");

            return json_encode(array("code" => $this->statusCode), JSON_NUMERIC_CHECK);
        }
    }

0 个答案:

没有答案