I am trying to send web request using Swift to get image data in JSON format, the code in the backend using PHP, so my PHP code looks like
function readImage($productID, $conn){
$query = "SELECT * FROM ProductImage WHERE ProductID='" . $productID . "'";
$result = $conn->query($query);
$i = 0;
while($row = $result->fetch_assoc())
{
$imageDatas = $row["ImageData"];
$imageDatas = base64_encode($imageDatas);
$i = $i + 1;
}
if($i == 0){
return array("Error" => true);
}
else{
$response = array("Error" => false);
$response["ImageDatas"] = $imageDatas;
$result = array("Response" => $response);
return json_encode($result);
}
}
And I am using PostMan to test my API and when I send request to retrieve the image using PostMan, it works fine and the result looks like
{
"Response": {
"Error": false,
"ImageDatas": "very long string (image data)"
}
}
However, in my Swift code, when I get the request response and try to convert the data to JSON format I get the following error:
(NSError?) error = domain: "NSCocoaErrorDomain" - code: 3840 {
ObjectiveC.NSObject = {}
}
I have googled this error and people say the returned data is not in proper JSON format, where my Swift code looks like
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
var error: NSError?
self.jsonResponse = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as! Dictionary <String, AnyObject>
}
Any idea what's going on? Any little help would be appreciated, as I run out of options.
Thanks