I tried to solved an issue regarding file upload with Alamofire (Link) (Swift 3.0) and retrieving them server-side using Slim PHP (Link) micro framework.
I'm trying to upload picture taken from the iPhone using
Alamofire.upload(multipartFormData:{
multipartFormData in
multipartFormData.append("value".data(/* ... */)!, withName :"key")
var idx = 1;
for imageRepresentation in imageData {
let pictureName = "pictures[]"
multipartFormData.append(imageRepresentation, withName: pictureName, mimeType: "image/jpeg")
idx += 1
}
},
to: uploadUrl,
method:.post,
headers: httpHeaders,
encodingCompletion: /* ... */
Here i'm almost sure that this script is working fine because when I hit https://httpbin.org/post I get back the encoded data base 64 image I've uploaded, so I'm quite sure that the issue comes from my server side code.
So, as I said, I'm using Slim PHP (Link) server side with this route
$this->post('/upload', function ($request, $response, $args) {
$request->getParsedBody(); //null
$request->getQueryParams(); // []
$request->getBody(); // {}
$request->getUploadedFiles(); // []
return /*Some JSON */
})->setName('upload');
Did I miss something ? Is there something I didn't understand ?
I already tried
Multipart/form-data example in slim micro framework
https://akrabat.com/psr-7-file-uploads-in-slim-3/
And the most weird thing is that the script works like a charm when executed from Paw API Explorer
Any help would be really appreciated ! Thanks.
For Uploading the image selected from photo library
In Swift 3 and Alamofire 4
Here is the Full Implementation of how to upload using Alamofire
Add the Following to your ViewController Class:
UIImagePickerControllerDelegate and UINavigationControllerDelegate
Create A Button:
First Create a button and implement the Following method in it for picker view
#IBAction func btnSelectProfileImageClicked(_ sender: Any) {
let ImagePicker = UIImagePickerController()
ImagePicker.delegate = self
ImagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.present(ImagePicker, animated: true, completion: nil)
}
Then Implement the following UIPicker Methods:
func imagePickerController( _ picker: UIImagePickerController,didFinishPickingMediaWithInfo info:[String : Any] )
{Imgprofile.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismiss(animated: true, completion: nil)}
Make Another Button Which Passes the data to URL using Alamofire and Give an #IBAction outlet to it to it :
Enter Following Data to it
#IBAction func btnUpdateProfileSelected(_ sender: Any) {
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.Imgprofile.image!, 1)!, withName: "Prescription", fileName: "Profile_Image.jpeg", mimeType: "image/jpeg")
}, to:" Your URL Here where You want to Upload")
{ (result) in
switch result {
case .success(let upload, _, _):
print(result)
upload.uploadProgress(closure: { (progress) in
print(progress)
})
upload.responseJSON { response in
//print response.result
print(response);
}
case .failure(let encodingError):
print(encodingError);
}
}
}
Thats all
Hope This helps
For Full sample code or any doubts please comment. I will provide you
the sample code for this. Which Includes the Fetching as well as
upload data using Alamofire.
Thanks
Related
I am trying to make a request to a PHP server from my swift app. For some reason php is showing an empty array as the $_REQUEST variable. I have looked through stack overflow and implemented everything I can find that might help, but still getting an empty array in php. Here is the relevant swift code...
func connect(_ pin: String, completion: #escaping(Result<ConnectResponse?, Error>) -> ()) {
let params: [String : Any] = [
"mobile_pin_connect": pin,
"device_info": UIDevice().model,
"additional_info": UIDevice().systemVersion
]
doRequest(params: params) { (data) in
if let data = data {
do {
let res = try JSONDecoder().decode(Dictionary<String, String>.self, from: data)
completion(.success(
ConnectResponse(success: (res["success"] == "true"), connect_id: res["connect_id"] ?? nil, error: res["error"] ?? nil)))
} catch {
completion(.failure(error))
}
} else {
print("in else block")
}
}
}
fileprivate func doRequest(params: [String: Any], completion: #escaping (Data?) -> ()) {
let body = createJsonBody(params)!
self.request.httpBody = body
print("Sending request with thw following variables")
print(String(data: body, encoding: .utf8)!)
print(String(data: self.request.httpBody!, encoding: .utf8))
URLSession.shared.dataTask(with: self.request) { (data, response, error) in
if let error = error {
print("Error in request: \(error)")
completion(nil)
}
let stringResult = String(data: data!, encoding: .utf8)!
let properResult = String(stringResult.map {
$0 == "." ? "=" : $0
})
let decodedData = Data(base64Encoded: properResult)
completion(decodedData)
}.resume()
}
fileprivate func createJsonBody(_ params: [String: Any]) -> Data? {
do {
let jsonData = try JSONSerialization.data(withJSONObject: params)
let body = Data(jsonData).base64EncodedData()
return body
} catch {
print("Unable to create json body: " + error.localizedDescription, error)
return nil
}
}
That sends the request to the server, the setup for the request is in the static var setup...
private static var sharedConnector: ApiConnector = {
let url = URL(string: "https://mywebsiteURLhere.com/api/mobile/challenge")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let connector = ApiConnector(request)
return connector
}()
So I have the right header values for application/json I have the request method set to post, I am base64encoding the json data and in PHP I have the setup getting php://input...
$rawRequest = file_get_contents("php://input");
and dumping the $_REQUEST variable to an error log, but I always get array\n(\n)\n
it is just showing an empty array
I even did
error_log("Raw request from index.php");
error_log(print_r($rawRequest, true));
and it logs a completely empty line.
I can't figure out why PHP is getting nothing in the request, from everything I have seen online I am doing the request correctly in swift. Any help is really appreciated. Thank you
As per your Swift Code, Can you please replace the following method.
fileprivate func createJsonBody(_ params: [String: Any]) -> Data? {
do {
let jsonData = try JSONSerialization.data(withJSONObject: params)
let body = Data(jsonData)
return body
} catch {
print("Unable to create json body: " + error.localizedDescription, error)
return nil
}
}
You need to replace this line let body = Data(jsonData) with
let body = Data(jsonData).base64EncodedData()
Without seeing your PHP code, it is difficult to determine the entire picture. However, whatever steps you perform to encode your data via the client (Swift) you must reverse to successfully decode the message on the server.
For example, if you prepare and send the request from your client as follows.
Client:
JSON encode data
base-64 encode
send data
The your server must reverse the steps to successfully decode the data.
Server:
recv data
base-64 decode data
JSON decode data
Unless your server requires it, I would remove the base-64 encode step, as it only complicates your encode / decode process.
I have created a working example: https://github.com/stuartcarnie/stackoverflow/tree/master/q59329179
Clone it or pull down the specific code in your own project.
To test, open up a terminal and run the php server:
$ cd q59329179/php
$ php -S localhost:8080 router.php
PHP 7.3.9 Development Server started at Thu Dec 19 10:47:58 2019
Listening on http://localhost:8080
Document root is /Users/stuartcarnie/projects/stackoverflow/q59329179/php
Press Ctrl-C to quit.
Test it works with curl in another terminal session:
$ curl -XPOST localhost:8080 --data-binary '{"string": "foo", "number": 5}'
Note you should see output in the php session:
[Thu Dec 19 11:33:43 2019] Array
(
[string] => foo
[number] => 5
)
Run the Swift test:
$ cd q59329179/swift
$ swift run request
Note again, decoded output in php session:
[Thu Dec 19 11:20:49 2019] Array
(
[string] => string value
[number] => 12345
[bool] =>
)
Your request is probably not arriving through the POST structure, but is kept in the request body.
Try running this as your first PHP operation:
$raw = file_get_contents('php://input');
and see what, if anything, is now into $raw. You should see a Base64 encoded string there, that you need to decode - like this, if you need an array:
$info = json_decode(base64_decode($raw), true);
I've tested your code and it's working fine. The issue might be at your PHP end. I've tested the following code on local server as well as on httpbin
The output from a local server (recent version of XAMPP (php 7.3.12)):
Sending request with thw following variables
eyJhZGRpdGlvbmFsX2luZm8iOiIxMy4yLjIiLCJtb2JpbGVfcGluX2Nvbm5lY3QiOiIxMjM0IiwiZGV2aWNlX2luZm8iOiJpUGhvbmUifQ==
result eyJhZGRpdGlvbmFsX2luZm8iOiIxMy4yLjIiLCJtb2JpbGVfcGluX2Nvbm5lY3QiOiIxMjM0IiwiZGV2aWNlX2luZm8iOiJpUGhvbmUifQ==
message ["additional_info": "13.2.2", "mobile_pin_connect": "1234", "device_info": "iPhone"]
Code:
ApiConnector.swift
import Foundation
import UIKit
class ApiConnector{
var request: URLRequest
private init(request: URLRequest) {
self.request = request
}
public static var sharedConnector: ApiConnector = {
let url = URL(string: "http://localhost/post/index.php")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let connector = ApiConnector(request: request)
return connector
}()
func connect(_ pin: String, completion: #escaping(Result<Dictionary<String, String>, Error>) -> ()) {
let params: [String : Any] = [
"mobile_pin_connect": pin,
"device_info": UIDevice().model,
"additional_info": UIDevice().systemVersion
]
doRequest(params: params) { (data) in
if let data = data {
do {
let res = try JSONDecoder().decode(Dictionary<String, String>.self, from: data)
completion(.success(res))
} catch {
completion(.failure(error))
}
} else {
print("in else block")
}
}
}
fileprivate func doRequest(params: [String: Any], completion: #escaping (Data?) -> ()) {
let body = createJsonBody(params)!
self.request.httpBody = body
print("Sending request with thw following variables")
print(String(data: body, encoding: .utf8)!)
URLSession.shared.dataTask(with: self.request) { (data, response, error) in
if let error = error {
print("Error in request: \(error)")
completion(nil)
}
let stringResult = String(data: data!, encoding: .utf8)!
print("result \(stringResult)")
let properResult = String(stringResult.map {
$0 == "." ? "=" : $0
})
let decodedData = Data(base64Encoded: properResult)
completion(decodedData)
}.resume()
}
fileprivate func createJsonBody(_ params: [String: Any]) -> Data? {
do {
let jsonData = try JSONSerialization.data(withJSONObject: params)
let body = Data(jsonData).base64EncodedData()
return body
} catch {
print("Unable to create json body: " + error.localizedDescription, error)
return nil
}
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
let session = URLSession.shared
override func viewDidLoad() {
super.viewDidLoad()
ApiConnector.sharedConnector.connect("1234") { (result) in
switch result {
case .success(let message):
print("message \(message)")
case .failure(let error):
print(error.localizedDescription)
}
}
}
}
index.php
echo file_get_contents("php://input");
You can verify your code by doing a request to https://httpbin.org/post
output:
Sending request with thw following variables
eyJkZXZpY2VfaW5mbyI6ImlQaG9uZSIsImFkZGl0aW9uYWxfaW5mbyI6IjEzLjIuMiIsIm1vYmlsZV9waW5fY29ubmVjdCI6IjEyMzQifQ==
result {
"args": {},
"data": "eyJkZXZpY2VfaW5mbyI6ImlQaG9uZSIsImFkZGl0aW9uYWxfaW5mbyI6IjEzLjIuMiIsIm1vYmlsZV9waW5fY29ubmVjdCI6IjEyMzQifQ==",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-us",
"Content-Length": "108",
"Content-Type": "application/json; charset=utf-8",
"Host": "httpbin.org",
"User-Agent": "SessionTest/1 CFNetwork/1120 Darwin/19.0.0"
},
"json": null,
"origin": "122.173.135.243, 122.173.135.243",
"url": "https://httpbin.org/post"
}
in else block
If you are running an older version of PHP then You might need HTTP_RAW_POST_DATA
Have look at this SO for more info on PHP side.
I am trying to upload file to server. I am using SlimFramework and PHP in my backend code and it is in RESTful form. Image uploading works very well if I upload it through Postman however I am not able to get it to work using Swift and Alamofire library.
In the route I have in backend, I get the file like this, from body parameter:
$image = $request->getUploadedFiles();
and to upload I use the:
$image->moveTo("myPath");
In Swift I have router, which gives .post method and the route URL and I tried like this:
func uploadImage(user_id: Int, image: Data, completion: #escaping (_ error: Error?, _ success: Bool)->Void) {
let parameters = [
"user_id": user_id,
"newFile": image
] as [String : Any]
Alamofire.request(Router.imageUplaod(parameters: parameters))
.validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result{
case .failure(let error):
completion(error, false)
print(error)
case .success(let value):
//Registered sucesfully! let json = JSON(value)
completion(nil, true)
}//Switch case
}//Alamofire
}
And I call it like this:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenProfileImage = info[UIImagePickerControllerEditedImage] as! UIImage
let imageData = UIImageJPEGRepresentation(chosenProfileImage, 0.2)
profileImageView.image = chosenProfileImage
dismiss(animated: true) {
API.uploadImage(user_id: User.sharedInstance.user_id, image: imageData!) { (error, success) in
if success{
print("IMAGE UPLOADDED")
}
}
}
}
This is my backend route:
$this->post('/image/upload', function($request, $response, $args){
global $database;
$data = $request->getParsedBody();
$user_id = $data['user_id'];
$files = $request->getUploadedFiles();
if (empty($files['newfile'])) {
throw new Exception('Expected a newfile');
}
$newfile = $files['newfile'];
if ($newfile->getError() === UPLOAD_ERR_OK) {
$uploadFileName = "profileImage".$user_id.".jpg";
$newfile->moveTo("/path/to/image/$uploadFileName");
}
$database->query("UPDATE `users` SET `profile_image_url` = :image WHERE user_id = :user_id");
$database->bind(':image', $uploadFileName);
$database->bind(':user_id', $user_id);
$database->execute();
$Jsonarray['status'] = "true";
$Jsonarray['message'] = "Image Uploaded Successfully";
return $response->withStatus(200)->withHeader('Content-Type', 'application/json')->write(json_encode($Jsonarray));
});
Weird thing is that Alamofire outputs status code 500 but the server do not tell me nothing about it. The problem must be in the Swift part. So I do not know even how to debug it.
How should I solve the problem?
I solved the problem. I rebuilt it that way:
Convert image to Base64 String on client
Decode it to image on backend
Upload it
So in Swift I Convert it like this:
let chosenProfileImage = info[UIImagePickerControllerEditedImage] as! UIImage
let imageData = UIImageJPEGRepresentation(chosenProfileImage, 0.2)
profileImageView.image = chosenProfileImage
let base64String = imageData?.base64EncodedString(options: .lineLength64Characters)
And backend is pretty much same, just converted it back to file using base64_decode(); and used file_put_contents(); function to move it to server.
I am having trouble using URLSession to access JSON data generated by a PHP file.
I am both confused about the Swift 3 syntax and the completion handlers.
So far I have:
override func viewDidLoad() {
super.viewDidLoad()
var data : NSMutableData = NSMutableData()
let urlString: String = "http://seemeclothing.xyz/service.php"
let urlObject: URL = URL(string: urlString)!
let response = URLResponse
let config = URLSessionConfiguration.default
let sessionObject: URLSession
sessionObject.dataTask(with: urlObject) { (Data?, URLResponse?, Error?) in
print(data)
print(URLResponse)
print(Error)
}
sessionObject.resume()
}
I want sessionObject to go to my server and print JSON data from PHP file.
Any help would be greatly appreciated.
I would strongly suggest using Alamofire. It abstracts away all the tedious stuff in URLSession. I switched to this a few days ago and am a happy camper since then.
For your problem:
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
I've looked up other solutions to this question but I don't fully understand what they're doing, and I can't get mine to work.
Here's my swift code
let imageData = UIImageJPEGRepresentation(image, 1.0)
if(imageData == nil ) { return }
let request = NSMutableURLRequest(URL: NSURL(string: ip)!) //ip is a string variable holding my correct ip address
request.HTTPMethod = "POST"
request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")
let postString = "id=\(id)&"
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
let body = NSMutableData()
body.appendData(postString.dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(imageData!)
request.HTTPBody = body
let task = session.uploadTaskWithRequest(request, fromData: imageData!)
task.resume()
And here's my PHP file
<?php
if (move_uploaded_file($_FILES['file']['tmp_name'], "image.jpg")) {
echo "File uploaded: ".$_FILES["file"]["name"];
}
else {
echo "File not uploaded";
}
?>
I have valid read and write access to the "image.jpg" file which sits on the front of my server, but it will still say that it could not upload the file. Any thoughts?
You're submitting the image as part of the POST request body. It won't be accessible using $_FILES.
You can Base-64 encode the image data, send the post string "id=\(id)&image=\(base64EncodedImageData)", then retrieve and decode it using $_POST.
You may want to consider using a networking library like Alamofire.
The manual and not recommended way: Change your PHP code to generate JSON responses.
echo json_encode(array("success" => true,
"filename" => basename($_FILES['image']['name']));
But as was mentioned you should let Alamofire do that for you. You can use MSProgress for a visual progress update in Alamofire.
let apiToken = "ABCDE"
Alamofire.upload(
.POST,
"http://sample.com/api/upload",
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imageData, name: "yourParamName", fileName: "imageFileName.jpg", mimeType: "image/jpeg")
multipartFormData.appendBodyPart(data: apiToken.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"api_token")
multipartFormData.appendBodyPart(data: otherBodyParamValue.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"otherBodyParamName")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
print("Uploading Avatar \(totalBytesWritten) / \(totalBytesExpectedToWrite)")
dispatch_async(dispatch_get_main_queue(),{
/**
* Update UI Thread about the progress
*/
})
}
upload.responseJSON { (JSON) in
dispatch_async(dispatch_get_main_queue(),{
//Show Alert in UI
print("Avatar uploaded");
})
}
case .Failure(let encodingError):
//Show Alert in UI
print("Avatar uploaded");
}
}
);
Working on a Swift project, and apparently the file I'm uploading isn't an image. I'm also using the Alamofire utility.
let uuid = NSUUID().UUIDString
print ("MARK -- UUID is " + uuid)
let image = imageView.image
Alamofire.upload(.POST, "{RETRACTED}", multipartFormData: {
multipartFormData in
if let imageData = UIImageJPEGRepresentation(image!, 0.6) {
multipartFormData.appendBodyPart(data: imageData, name: uuid, fileName: uuid + ".png", mimeType: "image/png")
}
}, encodingCompletion: {
encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print ("JSON \(JSON)")
self.displayAlert("Uploaded!", message: "{RETRACTED}", responseButtonText: "{RETRACTED}")
}
}
case .Failure(let encodingError):
print(encodingError)
}
})
Here's the JSON response (I also have the PHP server side code if needed):
JSON {
response = "That file wasn't an image (Only .png and .jpg/.jpeg images are accepted). The type is ";
responseCode = 0;
}
Thanks guys.
You are sending an jpg image, but specifying mime type for png. Change mime type to "image/jpeg" or send a png file.