Xcode und Localhost Request JSON?

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Hier mal ein Beispiel wie es im moment ist

    Quellcode

    1. // Autorization Start
    2. func startAutorization() {
    3. // prepare json data
    4. let json: [String: Any] = ["email": "test@test.de",
    5. "password": "Test-01."]
    6. let jsonData = try? JSONSerialization.data(withJSONObject: json)
    7. // create post request
    8. let url = URL(string: "http://10.7.101.107:5000/api/login")!
    9. var request = URLRequest(url: url)
    10. request.httpMethod = "POST"
    11. // insert json data to the request
    12. request.httpBody = jsonData
    13. let task = URLSession.shared.dataTask(with: request) { data, response, error in
    14. guard let data = data, error == nil else {
    15. print(error?.localizedDescription ?? "No data")
    16. return
    17. }
    18. let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
    19. if let responseJSON = responseJSON as? [String: Any] {
    20. print(responseJSON)
    21. }
    22. }
    23. task.resume()
    24. }
    25. }
    Alles anzeigen
  • Dann versuche es doch mal so: ;)

    Quellcode

    1. // Autorization Start
    2. func startAutorization() {
    3. // prepare json data
    4. let json: [String: Any] = ["email": "test@test.de",
    5. "password": "Test-01."]
    6. let jsonData = try? JSONSerialization.data(withJSONObject: json)
    7. // create post request
    8. let url = URL(string: "http://10.7.101.107:5000/api/login")!
    9. var request = URLRequest(url: url)
    10. request.httpMethod = "POST"
    11. // insert json data to the request
    12. request.httpBody = jsonData
    13. let task = URLSession.shared.dataTask(with: request) { data, response, error in
    14. print(String.init(data:data encoding:utf8))
    15. print(response)
    16. print(error)
    17. guard let data = data, error == nil else {
    18. print(error?.localizedDescription ?? "No data")
    19. return
    20. }
    21. let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
    22. if let responseJSON = responseJSON as? [String: Any] {
    23. print(responseJSON)
    24. }
    25. }
    26. task.resume()
    27. }
    28. }
    Alles anzeigen
  • So hat es leider auch nicht Funktioniert.

    Aber jetzt habe ich die Lösung, deine Tipp (Zitat) war richtig.

    MCDan schrieb:

    Ich tippe mal, dass im completionHandler error nicht nil ist. Gib doch auch mal response aus und schaue, ob da noch Infos enthalten sind.

    Alternativ setze auch mal per setValue(_:forHTTPHeaderField:) den Content-Type auf application/json.

    setValue gesetzt

    Quellcode

    1. request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    Jetzt Funktioniert die Ausgabe in der Console wie ich das brauche.
    jedenfalls bin ich einen Riesen schritt weiter, Danke

    PS. bei Interesse kann ich hier die Komplette Methode posten

    Gruß
  • Nun kommen wir zur nächsten Methode.
    von der "Autorization" Methode bekomme ich den gewünschten Apitoken, in der nächsten Methode möchte ich einen "GET" Request an den Server(Lokal-Test) schicken.
    Beim App Start wird also eine Autorisierung gemacht aus der ich ein apitoket bekomme, der wird in globale variable gespeichert.
    Danach scanne ich das passende QR Code, Inhalt des QR codes ist die MachineID.
    Die MachineID ist im Switch Case angelegt.
    Aus dem Token + ID wird ein GET Request an den Server geschickt (Dazu gibt es eine API die JSON zurück liefert)

    Wenn ich das jetzt mit einem Tool durchspiele bekomme ich den gewünschten JSON zurück
    Hier siehe Screenshot zb.
    Bildschirmfoto 2018-10-04 um 14.35.15.png

    Jetzt zu meinem Problem
    Ich habe das ganze in Xcode gemacht, bekomme auch Status 200 (Die Anfrage wurde erfolgreich bearbeitet und das Ergebnis der Anfrage wird in der Antwort übertragen.) wieder.
    Aber JSON Antwort nicht in Xcode console .

    Beispiel wo ich grade stehe, mein Coding, ist noch nicht clean, evtl auch falsch...

    Quellcode

    1. // Get Machine Data
    2. func getMachineData (machineId: String) {
    3. if(self.appDelegate.apiToken != nil && self.appDelegate.apiToken != ""){
    4. // // prepare json data
    5. // let json: [String: Any] = ["email": "test@test.de",
    6. // "password": "Test-01."]
    7. //
    8. // let jsonData = try? JSONSerialization.data(withJSONObject: json)
    9. //
    10. // // Request Print in Console
    11. // let responseJSON = try? JSONSerialization.jsonObject(with: jsonData!, options: [])
    12. // if let responseJSON = responseJSON as? [String: Any] {
    13. // print(responseJSON)
    14. // }
    15. // Create post request
    16. let url = URL(string: "http://10.7.100.100:5000/api/v1/reweivobject/"+machineId)!
    17. print(url)
    18. //let url = URL(string: "http://10.7.100.100:5000/api/test/echo?message=Hello")!
    19. //print(url)
    20. //let url = URL(string: "http://10.7.100.100:5000/api/test/time")!
    21. // print(url)
    22. var request = URLRequest(url: url)
    23. request.httpMethod = "GET"
    24. request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    25. request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
    26. request.setValue("Bearer "+self.appDelegate.apiToken!, forHTTPHeaderField: "Authorization")
    27. // insert json data to the request
    28. // request.httpBody = jsonData
    29. let config = URLSessionConfiguration.default
    30. let session = URLSession(configuration: config)
    31. let task = session.dataTask(with: request) { (responseData, response, responseError) in
    32. guard responseError == nil else {
    33. print(responseError!)
    34. return
    35. }
    36. let httpResponse = response as! HTTPURLResponse
    37. let statusCode = httpResponse.statusCode
    38. print(statusCode)
    39. print(response!.description) //(von ? auf !)
    40. print(responseData!.description) //(von ? auf !)
    41. let type = httpResponse.allHeaderFields["Content-Type"]
    42. print("Content-Type", type) // <--- Force Unwrap (!)
    43. let l = httpResponse.allHeaderFields["Content-Length"]
    44. print("Content-Length", l) // <--- Force Unwrap (!)
    45. // APIs usually respond with the data you just sent in your POST request
    46. if statusCode == 200{
    47. if let data = responseData { _ = try? JSONSerialization.jsonObject(with: data, options: []) as? [String:Any];
    48. // let name = json??["name"] as! String
    49. // let userid = json??["userid"] as! String
    50. // print(name)
    51. // print(userid)
    52. } else {
    53. print("no readable data received in response")
    54. }
    55. } else {
    56. print("Authentifizierung fehlgeschlagen")}
    57. }
    58. task.resume()
    59. }
    60. }
    Alles anzeigen
    In der Console bekomme ich nur das hier:

    <NSHTTPURLResponse: 0x2814ed800> { URL: 10.7.100.100:5000/api/v1/rewei…95-4935-98a7-72b5a68c95bf } { Status Code: 200, Headers {
    "Content-Type" = (
    "application/json; charset=utf-8"
    );
    Date = (
    "Thu, 04 Oct 2018 13:28:02 GMT"
    );
    Server = (
    Kestrel
    );
    "Transfer-Encoding" = (
    Identity
    );
    } }
    554 bytes

    Sollte aber eigentlich den JSON bekommen wie oben im Screenshot vom Tool...



    Habt ihr irgendwelche Ideen?

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von jp.graf ()

  • @jp.graf Kurz vorweg: Du benutzt in Deinem Code viel zuviele überflüssige lokale Variablen. 'Variable shadowing' ist da Dein Freund. Außerdem verwendest Du dauernd forced unwrapped optionals, aka !. Das ist ganz böse.

    Ich hab mal auf Grundlage Deines Codes eine kompaktere Variante zusammengehackt, die auf einen offen zugänglichen Testserver zugreift. Copy & Paste in ein Playground und es läuft.

    Quellcode

    1. import Foundation
    2. let url = URL(string: "https://httpbin.org/json")!
    3. var request = URLRequest(url: url)
    4. request.httpMethod = "GET"
    5. request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    6. request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
    7. func foo() {
    8. let session = URLSession(configuration: .default)
    9. let task = session.dataTask(with: request) { (data, response, error) in
    10. if let error = error {
    11. print(error)
    12. return
    13. }
    14. guard
    15. let response = response as? HTTPURLResponse,
    16. let data = data
    17. else {
    18. print("No response or no data!")
    19. return
    20. }
    21. let statusCode = response.statusCode
    22. // diagnostics
    23. let responseType = response.allHeaderFields["Content-Type"]
    24. let responseLength = response.allHeaderFields["Content-Length"]
    25. print("response code: \(statusCode)")
    26. print("Content-Type:", responseType ?? "")
    27. print("Content-Length:", responseLength ?? 0)
    28. print("Status code:", statusCode)
    29. switch statusCode {
    30. case 200:
    31. guard let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: Any] else {
    32. print("could not deserialize the json data!")
    33. return
    34. }
    35. print(json)
    36. case 300...399:
    37. print("Redirection")
    38. case 400...499:
    39. print("Client error")
    40. default:
    41. print("whatever…")
    42. }
    43. }
    44. task.resume()
    45. }
    46. foo()
    Alles anzeigen
    Vielleicht hilft's ja…
    Das iPhone sagt: "Zum Antworten streichen". Wie? Echt Jetzt? Muß ich erst die Wohnung streichen!?
  • torquato schrieb:

    request.httpMethod = "GET"
    ist eh Standard, braucht man nücht :)


    torquato schrieb:

    Ich hab mal auf Grundlage Deines Codes eine kompaktere Variante zusammengehackt, die auf einen offen zugänglichen Testserver zugreift. Copy & Paste in ein Playground und es läuft

    Hast du das selbst im Playground getestet? Mit älteren Versionen hat man doch immer noch


    Quellcode

    1. import PlaygroundSupport
    und wenn fertig ein

    Quellcode

    1. PlaygroundPage.current.needsIndefiniteExecution = true
    gebraucht. Lass mich da aber gern eines besseren belehren^^
  • matz schrieb:

    torquato schrieb:

    request.httpMethod = "GET"
    ist eh Standard, braucht man nücht :)

    Ah, OK. Danke. Ich bin mit diesen APis nicht so vertraut. Hab's einfach so vom OP übernommen.

    matz schrieb:


    torquato schrieb:

    Ich hab mal auf Grundlage Deines Codes eine kompaktere Variante zusammengehackt, die auf einen offen zugänglichen Testserver zugreift. Copy & Paste in ein Playground und es läuft
    Hast du das selbst im Playground getestet? Mit älteren Versionen hat man doch immer noch

    Quellcode

    1. import PlaygroundSupport
    und wenn fertig ein

    Quellcode

    1. PlaygroundPage.current.needsIndefiniteExecution = true
    gebraucht. Lass mich da aber gern eines besseren belehren^^

    Ja. Selber so im Playground geschrieben. Xcode 10.1 beta 1.

    Jetzt, wo Du das ansprichst. Stimmt. Das hätte sonst eigentlich nötig sein müssen. Ich hab mich manchmal schon gewundert, bzw. eher geärgert, warum in Xcode 10 ein Playground einfach weiterläuft, anstatt wie gewohnt nach code execution zu stoppen. Jetzt klicke ich, anders als sonst, häufiger auf Stop.

    Da scheint in den Xcode 10 Playgrounds etwas grundsätzlich geändert worden zu sein, ich könnte mich aber nicht daran erinnern, davon etwas in den Release Notes gelesen zu haben.

    Danke für den Hinweis. Das wäre mir sonst nicht so bewußt geworden.
    Das iPhone sagt: "Zum Antworten streichen". Wie? Echt Jetzt? Muß ich erst die Wohnung streichen!?
  • torquato schrieb:

    @jp.graf Kurz vorweg: Du benutzt in Deinem Code viel zuviele überflüssige lokale Variablen. 'Variable shadowing' ist da Dein Freund. Außerdem verwendest Du dauernd forced unwrapped optionals, aka !. Das ist ganz böse.

    Ich hab mal auf Grundlage Deines Codes eine kompaktere Variante zusammengehackt, die auf einen offen zugänglichen Testserver zugreift. Copy & Paste in ein Playground und es läuft.

    Quellcode

    1. import Foundation
    2. let url = URL(string: "https://httpbin.org/json")!
    3. var request = URLRequest(url: url)
    4. request.httpMethod = "GET"
    5. request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    6. request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
    7. func foo() {
    8. let session = URLSession(configuration: .default)
    9. let task = session.dataTask(with: request) { (data, response, error) in
    10. if let error = error {
    11. print(error)
    12. return
    13. }
    14. guard
    15. let response = response as? HTTPURLResponse,
    16. let data = data
    17. else {
    18. print("No response or no data!")
    19. return
    20. }
    21. let statusCode = response.statusCode
    22. // diagnostics
    23. let responseType = response.allHeaderFields["Content-Type"]
    24. let responseLength = response.allHeaderFields["Content-Length"]
    25. print("response code: \(statusCode)")
    26. print("Content-Type:", responseType ?? "")
    27. print("Content-Length:", responseLength ?? 0)
    28. print("Status code:", statusCode)
    29. switch statusCode {
    30. case 200:
    31. guard let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: Any] else {
    32. print("could not deserialize the json data!")
    33. return
    34. }
    35. print(json)
    36. case 300...399:
    37. print("Redirection")
    38. case 400...499:
    39. print("Client error")
    40. default:
    41. print("whatever…")
    42. }
    43. }
    44. task.resume()
    45. }
    46. foo()
    Alles anzeigen


    Vielleicht hilft's ja…
    Danke für die Tipps, versuche ich mit der zeit umzusetzen.

    Speziell mit dieser URL funktioniert es bei mir in XCODE auch.
    Siehe hier:

    Quellcode

    1. // Test GET Request (Funktioniert)
    2. func Test() {
    3. //create the url with NSURL
    4. let url = URL(string: "http://httpbin.org/get")! //change the url
    5. //create the session object
    6. let session = URLSession.shared
    7. //now create the URLRequest object using the url object
    8. let request = URLRequest(url: url)
    9. //create dataTask using the session object to send data to the server
    10. let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
    11. guard error == nil else {
    12. return
    13. }
    14. guard let data = data else {
    15. return
    16. }
    17. do {
    18. //create json object from data
    19. if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
    20. print(json)
    21. }
    22. } catch let error {
    23. print(error.localizedDescription)
    24. }
    25. })
    26. task.resume()
    27. }
    Alles anzeigen

    Mit meiner Test API bin ich jetzt soweit das ich eine JSON Antwort bekomme und in der Console ausgeben kann.
    Jetzt muss ich noch rausfinden wie ich auf ein bestimmtes Value innerhalb JSSON zugreifen kann...
    So sieht jetzt meine Raw JSON Antwort aus:

    Quellcode

    1. {"error":false,"result":{"id":"bb06424e-a795-4935-98a7-72b5a68c95bf","mandtID":"1e5eaf1a-31e7-4f76-b064-70ee1e2f0280","clientID":null,"name":"Name1","description":"Roboter","type":"KR150","manufacturer":"KUKA","startupDate":"2018-09-03T13:01:43.247499","regNumber":"235/KKD","purchPrice":100000.0,"purchDate":"2018-09-03T13:01:43.247501","serialNumber":234567891,"country":"Germany","place":"Stuttgart","costUnit":"KST 610","warrantyFrom":"2018-09-03T13:01:43.247503","warrantyTo":"2018-09-03T13:01:43.247504","objectDocuments":null,"objectGroups":null}}