Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3655

How API calls are done with headers in swift

$
0
0

I want to securely provide my data from the API using JWT token.So Currently I have implemented security in the backend and each ajax call eith the header

url:"https://dataurl,        contentType : "application/json; charset=utf-8",        headers : TokenHeader(),

But now I want to send the same data to be consumed by an IOS App which is developed in swift.I am not a IOS mobile developer and when I look into how they make the rest calls I found that something like this can be done:

let todosEndpoint: String = "https://dataurl"guard let todosURL = URL(string: todosEndpoint) else {  print("Error: cannot create URL")  return}var todosUrlRequest = URLRequest(url: todosURL)todosUrlRequest.httpMethod = "GET"let newTodo: [String: Any] = ["title": "My First todo", "completed": false, "userId": 1]let jsonTodo: Datado {  jsonTodo = try JSONSerialization.data(withJSONObject: newTodo, options: [])  todosUrlRequest.httpBody = jsonTodo} catch {  print("Error: cannot create JSON from todo")  return}let session = URLSession.sharedlet 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  }

So now my question is like ajax call in my web app how to consume rest service from the mobile app side. Any help is appreciated?


Viewing all articles
Browse latest Browse all 3655

Trending Articles