I want to send an array to my server with URLRequest post method but it doesn't work.
When I send a string it works well but with an array not work. I can't get data with array post.
Below code works great:
func getData() {
pageid = 1
var request = URLRequest(url: URL(string: "http://localhost/index.php")!, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60)
request.httpMethod = "POST"
let postString = "page=" + String(pageid);
request.httpBody = postString.data(using: .utf8)
URLSession.shared.dataTask(with:request, completionHandler: {(data, response, error) in
do {
let response = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! Dictionary<String, AnyObject>
let itemsArray = response["items"] as! [AnyObject]
for itemObj in itemsArray {
print(itemObj)
}
} catch {
}
}).resume();
}
my array is:
var ids: [Int] = [1, 5, 8]
i try to send it like this but it doesn't work
let postString = "page=" + String(ids.description)
my php code:
<?php
$pageid = $_POST['page'];
....
?>
Related
I have the following Swift 5 function that calls a PHP script on my server:
func getJSONdata(fileName:String, completion: (_ json:JSON)->()) {
let session = URLSession(configuration: .ephemeral)
var jsonData = JSON()
let myUrl = URL(string: DATABASE_PATH + fileName + "/query.php?queryAll");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
DispatchQueue.main.async {
let task = session.dataTask(with: request) { (data, response, error) in
if error != nil {
print("\(error!.localizedDescription)")
return
}
// Get data
jsonData = try! JSON(data: data!)
// print(jsonData)
}
task.resume()
}// ./ dispatch aync
completion(jsonData)
}
This function is hosted in a separate Swift file, now in my ViewController.swift I call that function as follows:
getJSONdata(fileName: "Users") { (jsonData) in
print("\(jsonData)")
}
In this case, I'm getting an empty array in my Xcode console, instead, if I uncomment the // print(jsonData) that's inside my getJSONdata() function, the console prints out my JSON data.
Obviously I'm doing something wrong in my getJSONdata() function because I cannot retrieve data by calling in ViewController.swift.
Where is the error in my function?
Try the below code. Maybe it will help you.
func getJSONdata(fileName:String, completion:#escaping (_ json:JSON)->()) {
let session = URLSession(configuration: .ephemeral)
var jsonData = JSON()
let myUrl = URL(string: DATABASE_PATH + fileName + "/query.php?queryAll");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
DispatchQueue.main.async {
let task = session.dataTask(with: request) { (data, response, error) in
if error != nil {
print("\(error!.localizedDescription)")
completion(nil)
}
// Get data
jsonData = try! JSON(data: data!)
// print(jsonData)
completion(jsonData)
}
task.resume()
}
}
I've found a solution, I don't know why but if I add #escaping to my function declaration, it works fine.
I also had to move completion(jsonData) below jsonData = try! JSON(data: data!), as suggested by #chirag90.
So, here's the complete function:
func getJSONdata(fileName:String, completion: #escaping (_ json:JSON?) -> Void) {
let session = URLSession(configuration: .ephemeral)
var jsonData = JSON()
let myUrl = URL(string: DATABASE_PATH + fileName + "/query.php?queryAll");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
DispatchQueue.main.async {
let task = session.dataTask(with: request) { (data, response, error) in
if error != nil {
self.simpleAlert("\(error!.localizedDescription)")
return
}
// Get data
jsonData = try! JSON(data: data!)
completion(jsonData)
}
task.resume()
}// ./ dispatch aync
}
i want to send a url.request(POST)with a Codable Struct like That :
struct Picklist : Codable {
var id = Int()
var ean = String()
var position = String()
}
and my request is like that :
let request = NSMutableURLRequest(url: NSURL(string: "http://XXX.XX.XX.XX/NEW/index.php")! as URL)
request.httpMethod = "POST"
let myID = Picklist.init(id: 3, ean: "asdf", position: "q")
let encoder = JSONEncoder()
do {
let jsonData = try encoder.encode(myID)
request.httpBody = jsonData
print("jsonData: ", String(data: request.httpBody!, encoding: .utf8) ?? "no body data")
} catch {
print("ERROR")
}
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response)")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(responseString)")
}
task.resume()
and of the Server Side i have this PHP Script:
<?php
print_r($_POST);
?>
but i am receiving :
responseString = Optional(Array
(
[{"id":3,"ean":"asdf","position":"q"}] =>
)
my Problem is that i can't get the Keys with the value on my PHP..
what is wrong with the code ?
Finally, answer is :
if you want to work with the response string just go and decode the "data"
For example :
guard let data = data else {return}
do{
let realData = try JSONDecoder().decode(response.self, from: data)
completion(realData)
}catch let jsonErr{
print(jsonErr)
}
in realData you can work with the data, but you need to create Codable/decobale Struct to work with
response is my coddle/decodable struct
or :
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
}
I am trying to make an api call I build the json object. But the data is not being passed I believe it is the formatting of the data when passed the service only has examples in php which i have posted below
API Docs: http://middleware.idxbroker.com/docs/api/methods/index.html#api-Leads-putLead
php call
// PUT lead in to IDX Broker
$url = 'https://api.idxbroker.com/leads/lead';
$data = array(
'firstName'=>$firstname,
'lastName'=>$lastname,
'email'=>$email
);
$data = http_build_query($data); // encode and & delineate
$method = 'PUT';
Swift Code to build json object
/// Create lead from text fields
let jsonObject: [String: String] = [
"firstName": (firstName.text! as String),
"lastNmae": lastName.text!,
"email": Email.text!,
"phoneNumber": phoneNumber.text!,
"city": (city.text as AnyObject) as! String,
"recieveUpdates": (switchIsChanged(recieveUpdates: recieveUpdates) as AnyObject) as! String
]
The API Call
class func putLead(lead: AnyObject){
let urlString = "https://api.idxbroker.com/leads/lead"
let url = NSURL(string: urlString)
print(lead)
var downloadTask = URLRequest(url: (url as URL?)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20)
/******************** Add Headers required for API CALL *************************************/
downloadTask.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
downloadTask.setValue(APICalls.getAccessKey(), forHTTPHeaderField: "accesskey")
downloadTask.setValue("json", forHTTPHeaderField: "outputtype")
downloadTask.httpMethod = "PUT"
downloadTask.httpBody = (lead as? Data)
/******************** End Headers required for API CALL *************************************/
URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in
/// Status Returned from API CALL
if let httpResponse = response as? HTTPURLResponse {
print("statusCode: \(httpResponse.statusCode)")
}
let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)
print(jsonData ?? "No Return")
}).resume()
/******** End URL Session **********/
}
Working Version
let newLead = "firstName=\(firstName.text!)&lastName=\(lastName.text!)&email=\(Email.text!)&phoneNumber=\(phoneNumber.text!)&city=\(String(describing: city.text))&recieveUpdates=\((switchIsChanged(recieveUpdates: recieveUpdates) as AnyObject) as! String)"
let newData = newLead.data(using: String.Encoding.utf8)
/// API Call with passing json String
APICalls.putLead(lead: newData!)
I was able to get it working by making the following changes to your putLead() function:
class func putLead(lead: [String:String]){
let urlString = "https://api.idxbroker.com/leads/lead"
let url = NSURL(string: urlString)
var httpBodyString = ""
for (key,value) in lead{
let currentKey = key.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
let currentValue = value.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
httpBodyString += "\(currentKey!)=\(currentValue!)&"
}
var downloadTask = URLRequest(url: (url as URL?)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20)
/******************** Add Headers required for API CALL *************************************/
downloadTask.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
downloadTask.setValue(<API_KEY_HERE>, forHTTPHeaderField: "accesskey")
downloadTask.httpMethod = "PUT"
downloadTask.httpBody = httpBodyString.data(using: String.Encoding.utf8)
/******************** End Headers required for API CALL *************************************/
URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in
/// Status Returned from API CALL
if let httpResponse = response as? HTTPURLResponse {
print("statusCode: \(httpResponse.statusCode)")
}
let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)
print(jsonData ?? "No Return")
}).resume()
/******** End URL Session **********/
}
I have a function that send two string values "Name and Text" to a server.
When the post method is sent the php file send an email with this two values.
Everything works if I send only one value from my iOS app however, if I try to send two or more value I don't get any email.
The code I am using is:
func postToServerFunction() {
var textFromapp: NSString = sendValueText.text
var nameFromapp: NSString = sendValueName.text
println("Button Pressed")
var url: NSURL = NSURL(string: "http://example.com//iOS/send_ios.php")!
var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
var bodyData = "data=" + (textFromapp as String) //data to send
var bodyName = "name=" + (nameFromapp as String) //data to send
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
request.HTTPBody = bodyName.dataUsingEncoding(NSUTF8StringEncoding);
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
println(response)
if let HTTPResponse = response as? NSHTTPURLResponse {
let statusCode = HTTPResponse.statusCode
if statusCode == 200 {
// Yes, Do something.
}
}
}
}
How could I tell the HTTPBody to look at both values?
for Future reference here how I solved it:
var bodyData = "data=" + (textFromapp as String) + "&name=" + (nameFromapp as String)
any other suggestion is welcome
Look at this answers. Fun(ctional) is the most beautiful.
function encodeData(data) {
return Object.keys(data).map(function(key) {
return [key, data[key]].map(encodeURIComponent).join("=");
}).join("&");
}
Hi I am trying to connect my iOS app to my PHP API.
I am sending JSON POST to my PHP API but I am getting an empty array as Output.
My Swift Code
#IBAction func JSONButtonAction(sender: AnyObject) {
var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
var session = NSURLSession(configuration: configuration)
var usr = "dsdd"
var pwdCode = "dsds"
var image : UIImage = clickedPhotoView.image!
var imageData = UIImagePNGRepresentation(image)
let base64String = imageData.base64EncodedStringWithOptions(.allZeros)
let params:[String: AnyObject] = [
"email" : usr,
"image" : base64String ]
let url = NSURL(string:"http://localhost/app/")
let request = NSMutableURLRequest(URL: url!)
let boundaryConstant = "Boundary-7MA4YWxkTLLu0UIW"; // This should be auto-generated.
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.allZeros, error: &err)
let task = session.dataTaskWithRequest(request) {
data, response, error in
// println("response = \(response)")
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("\(responseString)")
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode != 200 {
println("response was not 200: \(response)")
return
}
}
if (error != nil) {
println("error submitting request: \(error)")
return
}
// handle the data of the successful response here
var result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: nil) as? NSDictionary
//println(result)
}
task.resume()
}
PHP Code
print_r($_POST);
Output is
array(
)
But when I use
$data = json_decode(file_get_contents('php://input'), true);
It works fine
I dont know why $_POST is not working.
If your intent is actually to send a string, then you should change the content-type:
request.setValue("text/plain", forHTTPHeaderField: "Content-Type")
Tested your exact code with this modification on my tests server:
Otherwise, check #kekub's comment.