How to call URL in swift which contain square bracket[ ] - ios

I trying to call a web service which contain square brackets[ ].
But I don't know How do I call that api.
URL is shown below
http request method is GET.
https:"YOUR URL"/?searchCriteria[filter_groups][0][filters][0][field]=name&searchCriteria[filter_groups][0][filters][0][value]=(txtFldSearch.text!)&searchCriteria[filter_groups][0][filters][0][condition_type]=like
this is what I tried
#objc func getHintsFromTextField(textField: UITextField) {
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let url = URL(string: "https:"YOUR URL"/?searchCriteria[filter_groups][0][filters][0][field]=name&searchCriteria[filter_groups][0][filters][0][value]=%\(txtFldSearch.text!)%&searchCriteria[filter_groups][0][filters][0][condition_type]=like")!
let task = session.dataTask(with: url!) { data, response, error in
// ensure there is no error for this HTTP response
guard error == nil else {
print ("error: \(error!)")
return
}
// ensure there is data returned from this HTTP response
guard let content = data else {
print("No data")
return
}
// serialise the data / NSData object into Dictionary [String : Any]
guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
print("Not containing JSON")
return
}
print("gotten json response dictionary is \n \(json)")
// update UI using the response here
}
// execute the HTTP request
task.resume()
}
What's the right way to do this?.Please help. I am not able to call this api.

Related

Json response "GET" request: "No session Found" - Swift

I want to make a "GET" request to apache web server to retrieve some data. Before making the above request, I log in making a "POST" request, the web server opens a new session and I get the json response:
gotten json response dictionary is
["user": {
email = "asd#asd.it";
password = "<null>";\\ for security reason it doesn't return the password
sessionID = 6C61269BB7BB40682E96AD80FF8F1CB7;
}]
So far it's all correct. But then when I try to make the "GET" request to retrive the user's data, I get this response:
gotten json response dictionary is
["message": {
errorCode = F01;
errorDetails = "No session is found in the server, either you have not set the JSESSIONID cookie or the session does not exists";
message = "No session Found";
}]
The code for the "POST" request is:
let urlComp = NSURLComponents(string: "http://localhost:8080/mnemosyne/auth")!
let postDict: [String:Any] = ["email": "asd#asd.it", "password" : "password"]
var items = [URLQueryItem]()
for (key,value) in postDict {
items.append(URLQueryItem(name: key, value: value as? String))
}
urlComp.queryItems = items
var urlRequestAuth = URLRequest(url: urlComp.url!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10.0 * 1000)
urlRequestAuth.httpMethod = "POST"
let postData = try? JSONSerialization.data(withJSONObject: postDict, options: [])
urlRequestAuth.httpBody = postData
let taskAuth = URLSession.shared.dataTask(with: urlRequestAuth) { (data, response, error) in
guard error == nil else {
print(error as Any)
return
}
guard let content = data else {
print("No data")
return
}
guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
print("Not containing JSON")
return
}
print("gotten json response dictionary is \n \(json)")
}
taskAuth.resume()
This is the code for "GET":
let url = URL(string: "http://localhost:8080/mnemosyne/rest/task")
var request = URLRequest(url: url!)
let taskTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard error == nil else {
print ("error: \(error!)")
return
}
guard let content = data else {
print("No data")
return
}
guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
print("Not containing JSON")
return
}
print("gotten json response dictionary is \n \(json)")
}
taskTask.resume()
What's the problem? How can I pass in the request the session Id that I want to use?
I think you need send sessionID in your GET request.
I would at first try some util like Postman or RESTed free apps, to test request and understand how to send correct POST request.
Depending on server implementation - you could send session ID as a part of url, in POST body or in POST header.

Extra slash in GET request with Swift

I'm trying to make GET request. Url is configured without slash at the end of URL. Server receives url with "/" at the end so I get error 404. I can see response.url with slash, but urlRequest contains URL without it.
Can't understand, what's going on.
Meant to send:
http://someUrl.com/api
Sent:
http://someUrl.com/api/
Code sample
guard let url = URL(string: self.rootUrl + "/api") else {
print ("Can't make URL")
return
}
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "GET"
let sessionConf = URLSessionConfiguration.default
let session = URLSession.init(configuration: sessionConf)
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /api")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
print (urlRequest)
let json = try? JSONSerialization.jsonObject(with: responseData) as? [String: Any]
if ( json == nil ) {
print ("error json")
print(response!)
} else { ....
Swift cached response for this request, set CachePolicy to ignore reload and try to do GET request to other URL and than try again.

Swift 4 Properly Parse JSON String to Object

I'm trying to parse a JSON string coming from a URLSession request into a Swift object.
I managed to get the data for the first level properties but for nested properties something weird happens. Instead of : i get = AND strings are missing the double-quotes
How do I access the date property inside published because I can not do this: print(todo["published"]["date"])
Here is the data I get:
[
"pretty_artists": kida,
"published": {
date = "2015-12-05";
now = 1517005961;
time = "18.59";
timestamp = 1449341947;
},
"views": 36,
"yt_id": cyXbV7EUl14,
"play_start": 0,
"title": ski ide,
"duration": 235,
"video_name": skiide,
"artists": kida
]
Here is my function:
func makeGetCall(todoEndpoint: String) {
// Set up the URL request
guard let url = URL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /todos/1")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
// now we have the todo
// let's just print it to prove we can access it
print(todo["published"]["date"])
// the todo object is a dictionary
// so we just access the title using the "title" key
// so check for a title and print it if we have one
guard let todoTitle = todo["title"] as? String else {
print("Could not get todo title from JSON")
return
}
print("The title is: " + todoTitle)
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
try the SwiftyJSON library, it should help you parse the data more easily.
https://github.com/SwiftyJSON/SwiftyJSON

swift JSON login REST with post and get response example

It's my first experience with REST in iOS development with swift. I couldn't find any working or straight (simple) example for doing what i need here.
I have a login backend (https://myaddress.com/rest/login), where I need to pass 2 params: login and password. When I pass good values (user exists in database) I get 2 variables as a result: token (string) and firstLogin (bool). So when I get those values I know that login is successful and I can log in into my app.
So I am begging you for an example (just a simple function) of how to achieve that. If I get working code example I will know how to use it for other rest services in my app. I tried many solutions from tutorials I found, but any of them was working for me.. So to not waste my time searching I would like someone experienced to show me the way to achieve that.
I am not sure if Alamofire is so good to use, I know that swift 4 has it's own build neetwork services and to work with json. Any solution that works would be great.
Also, side question - if I would prefer to use Alamofire, do I need to use swiftyJSON also? Or it's just for parsing?
You can use URLSession if you don't like to import Alamofire in your Project to perform a simple task.
here are some method : GET, POST, DELETE METHODS and tutorial
GET METHOD
func makeGetCall() {
// Set up the URL request
let todoEndpoint: String = "https://jsonplaceholder.typicode.com/todos/1"
guard let url = URL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /todos/1")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
// now we have the todo
// let's just print it to prove we can access it
print("The todo is: " + todo.description)
// the todo object is a dictionary
// so we just access the title using the "title" key
// so check for a title and print it if we have one
guard let todoTitle = todo["title"] as? String else {
print("Could not get todo title from JSON")
return
}
print("The title is: " + todoTitle)
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
POST METHOD
func makePostCall() {
let todosEndpoint: String = "https://jsonplaceholder.typicode.com/todos"
guard let todosURL = URL(string: todosEndpoint) else {
print("Error: cannot create URL")
return
}
var todosUrlRequest = URLRequest(url: todosURL)
todosUrlRequest.httpMethod = "POST"
let newTodo: [String: Any] = ["title": "My First todo", "completed": false, "userId": 1]
let jsonTodo: Data
do {
jsonTodo = try JSONSerialization.data(withJSONObject: newTodo, options: [])
todosUrlRequest.httpBody = jsonTodo
} catch {
print("Error: cannot create JSON from todo")
return
}
let session = URLSession.shared
let task = session.dataTask(with: todosUrlRequest) {
(data, response, error) in
guard error == nil else {
print("error calling POST on /todos/1")
print(error!)
return
}
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let receivedTodo = try JSONSerialization.jsonObject(with: responseData,
options: []) as? [String: Any] else {
print("Could not get JSON from responseData as dictionary")
return
}
print("The todo is: " + receivedTodo.description)
guard let todoID = receivedTodo["id"] as? Int else {
print("Could not get todoID as int from JSON")
return
}
print("The ID is: \(todoID)")
} catch {
print("error parsing response from POST on /todos")
return
}
}
task.resume()
}
DELETE METHOD
func makeDeleteCall() {
let firstTodoEndpoint: String = "https://jsonplaceholder.typicode.com/todos/1"
var firstTodoUrlRequest = URLRequest(url: URL(string: firstTodoEndpoint)!)
firstTodoUrlRequest.httpMethod = "DELETE"
let session = URLSession.shared
let task = session.dataTask(with: firstTodoUrlRequest) {
(data, response, error) in
guard let _ = data else {
print("error calling DELETE on /todos/1")
return
}
print("DELETE ok")
}
task.resume()
}
Thanks #MAhipal Singh for you answer. I'll post here example with Alamafire that I used so it's all in one stack question. It's easier than I though, solutions I tried to use before were not working cause I had problems with pinning certificate about I forgot..
func loginRest(login:String, password:String, deviceId:String){
let urlStr = restServices.REST_MAIN_URL + restServices.REST_LOGIN
let params = ["login":login, "password":password, "deviceId":deviceId]
let paramsJson = try! JSONSerialization.data(withJSONObject: params)
var headers: HTTPHeaders = ["Content-Type": "application/json"]
Alamofire.request(urlStr, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
switch response.result {
case .success:
print("SUKCES with \(response)")
case .failure(let error):
print("ERROR with '\(error)")
}
}
If the post is proper the response is (console print):
SUKCES with SUCCESS: {
firstLogin = 1;
token = "dfkafjkfdsakfadsjfksjkfaadjfkjdfkjfskjfdkafjakfjakfjsafksjdafjy878328hjh";
}

error while doing JSON Serialization ErrorDomain Code=3840

I am trying to delete a post in a table view, but I get an error while doing JSON serialization, in this line
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject]
if i access the url through the browser, the data is successfully deleted in the database, and if it is accessed from the browser, it also gives the json response back like this
{"message":"successfully deleted","result":1,"status":"Image has been
deleted from drive"}
but i got an error that says the json text did not start with array or object (code =3840), but as you can see above, it is a json dictionary
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
here is the full code i use. what went wrong in here? Thanks
func deletePost(_ indexPath: IndexPath) {
let tweet = tweetsArray[indexPath.row]
let uuid = tweet["uuid"] as! String
let imagePath = tweet["imagePath"] as! String
let url = URL(string: "http://localhost/Twitter/post.php")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
let body = "uuid=\(uuid)&path=\(imagePath)"
request.httpBody = body.data(using: String.Encoding.utf8)
let session = URLSession.shared
let task = session.dataTask(with: request) { (data, response, error) in
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject]
guard let parsedJSON = json else {
print("error while parsing JSON")
return
}
let jsonMessage = parsedJSON["message"]
if jsonMessage != nil {
// hilangkan data di array
self.tweetsArray.remove(at: indexPath.row)
self.imagesArray.remove(at: indexPath.row)
// hilangkan rownya pada table view
self.tableView.deleteRows(at: [indexPath], with: .automatic)
self.tableView.reloadData()
}
}
catch {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = "\(error)"
self.showAlert(alertTitle: "sorry", alertMessage: message, actionTitle: "OK")
})
return
}
}
else {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = "\(error!.localizedDescription)"
self.showAlert(alertTitle: "sorry", alertMessage: message, actionTitle: "OK")
})
return
}
}
task.resume()
}
You can try to Print out your server response. Please change the code in your catch block like. and identifying an error in server side or not.
Your server data is proper json formate then print out your server data and check a server data is valid or not.
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let jsonData = data {
do {
let parsedData = try JSONSerialization.jsonObject(with: jsonData, options: .mutableLeaves) as! [String: AnyObject]
if let area = parsedData["AREA"] as? [[String: AnyObject]] {
for a in area {
print(a["area_name"])
print(a["price"])
}
}
}
catch let err{
print("\n\n===========Error===========")
print("Error Code: \(error!._code)")
print("Error Messsage: \(error!.localizedDescription)")
if let data = data, let str = String(data: data, encoding: String.Encoding.utf8){
print("Server Error: " + str)
}
debugPrint(error)
print("===========================\n\n")
debugPrint(err)
}
}
else {
debugPrint(error as Any)
}
}.resume()

Resources