简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM