i don't get data from my server

my link works if I use postman to check if I receive data for the server and it works but my code doesn't work

this is the data that I should receive

[
{ "id": "18", "profileName": "testProfiel24" },
{ "id": "19", "profileName": "testProfiel25" },
{ "id": "1021", "profileName": "testProfiel26" },
{ "id": "1022", "profileName": "testProfiel27" },
{ "id": "1023", "profileName": "testProfiel28" }
]

Code Block swift
func getData(){
     
    let getToken : String? = KeychainWrapper.standard.string(forKey: "accessToken")
     
     
    let url = URL(string: "http://......./profile/load")!
    let queryItems = [URLQueryItem(name: "token", value: getToken!)]
    let newUrl = url.appending(queryItems)!
    print(newUrl)
     
    URLSession.shared.dataTask(with: newUrl, completionHandler: { data, response, error in
     
          guard let data = data,error == nil else{
            print(error?.localizedDescription as Any)
            return
          }
     
          var result : Profile?
     
          do{
            result = try JSONDecoder().decode(Profile.self, from: data)
          }catch{
     
            print("failed to convert\(error.localizedDescription)")
     
          }
     
          guard let json = result else{
            return
          }
     
          print(json.id)
          print(json.profileName)
        }).resume()
     
  }


Code Block swift
extension URL {
  func appending(_ queryItems: [URLQueryItem]) -> URL? {
    guard var urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true) else {
      return nil
    }
    urlComponents.queryItems = (urlComponents.queryItems ?? []) + queryItems
    return urlComponents.url
  }
}


Code Block swift
struct Profile : Decodable {
  let id, profileName : String
   
}

my code doesn't work

That's not useful information.
  • Does it crash ? If so, where and what log message ?

  • Doesn't it provide the correct result ? If so, what do you get ?

  • Doesn't it provide any result at all ?If so, could you show the results of all the print statements ?

Also, check line 6:
Code Block
    let url = URL(string: "http://......./profile/load")!

replace http with https

this is the data that I should receive

The outermost structure of the data is JSON array as represented with [ and ].
So, you need to pass an Array type to decode.

At least, you need to change line 22 ... 34 of getData() as follows:
Code Block
var result : [Profile]?
do {
result = try JSONDecoder().decode([Profile].self, from: data)
} catch {
print("failed to convert\(error)")
}
guard let jsonArray = result else{
return
}
for json in jsonArray {
print(json.id)
print(json.profileName)
}



You also may need to check things as shown in Claude31's reply. The term doesn't work does not work when telling what's happening with your code.
@Claude 31

My server use http requset not https

I get a complete link to my server with the token but hi says "try printing with 'vo' or 'po' "

it doesn't crash but hi skips every code after line 22 the URLSession

I edit it

Code Block swift
           var result : [Profile]?
     
          do{
            result = try JSONDecoder().decode([Profile].self, from: data)
          }catch{
     
            print("failed to convert\(error.localizedDescription)")
     
          }
     
          guard let jsonArray = result else{
            return
          }
      for json in jsonArray{
        print(json.id)
        print(json.profileName)
      }
     
        }).resume()


but hi skips he URLSession


it doesn't crash but hi skips every code after line 22 the URLSession 
(Sorry, I do not understand what you mean by hi...)

Can you tell us how you have checked that every code after line 22 is skipped? (Have you put breakpoint somewhere?)
What's shown in the debug console? (Are you activated the debug console?)
@OOper


Can you tell us how you have checked that every code after line 22 is skipped? (Have you put a breakpoint somewhere?)
What's shown in the debug console? (Are you activated the debug console?)

I have used a breakpoint by the link so that I can see if the link correct

I have also used a breakpoint by the URLSeeion but I click on step into then hi send me to resume() function

This what i get in the console

self LinkMe.DashboardViewController 0x00007f957df250d0
UIKit.UIViewController UIViewController
vwDashboardView UIView? 0x00007f957de2fa00
lbLinkMe UILabel? 0x00007f957de2c8f0
lbWelkom UILabel? 0x00007f957de2e1f0
butNieuwProfile UIButton? 0x00007f957de2bb30
butAccount UIButton? 0x00007f957de28900
tabel UITableView? 0x00007f957f090000
data [String] 5 values
newUrl URL <unavailable; try printing with "vo" or "po">
url URL <unavailable; try printing with "vo" or "po">
getToken String? "232286f0-eb93-4509-8364-11dd1db5257e" some
queryItems [URLQueryItem] 1 value

This what i get in the console 

I'm afraid it is the info shown in the variables view in the left side of the debug area.

To activate the debug console, (from the main menu bar of Xcode)View > Debug Area > Activate Console.
Have you really done it?

I have also used a breakpoint by the URLSeeion but I click on step into then hi send me to resume() function

That is the right behavior of Xcode debugger when working with asynchronous code.
You cannot go into the completion handler using Step Into.
You need to put a breakpoint inside the completion handler.
Try putting a breakpoint at the line guard let data = data,error == nil else{.


One more.

My server use http requset not https 

To communicate with http servers, you need a proper ATS settings in your Info.plist.
Have you done it?

This what hi said in the debug console

http://....
2020-12-17 07:52:05.227926-0800 LinkMe[2285:99513] [] nwprotocolgetquicimageblockinvoke dlopen libquic failed
(lldb) 

l have added a breakpoint at the line
Code Block
guard let data = data, error ==nil else{


I have already added a proper ATS to settings

This what hi said in the debug console

That is not an important part, what do you get when you Continue?
i don't get data from my server
 
 
Q