Я получаю сообщение об ошибке при попытке отправить параметры через запрос POST на мой сервер через 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("===")
}
}
}
Я получаю ошибку:
> 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);
}
}
Задача ещё не решена.
Других решений пока нет …