douchui1488 2015-12-13 15:03
浏览 395

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

I receive an error when trying to send parameters via POST request to my server via Alamofire.

    // 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("===")
        }
    }
}

The error I receive is:

> 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]
> ===

I formated the parameters array correctly I would guess. Where is this error coming from?

The Server code:

// 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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。