2017-09-08 3 views
0

Ich mache eine sehr einfache postRequest, aber ich der Service antwortet mir nicht, haben Sie eine Ahnung, warum das passiert? Vielleicht mache ich etwas falsch, könntest du mir helfen? Danke im Voraus.Beitrag Anfrage antwortet nicht

Hier ist mein Code Request in postman

@IBAction func buton(_ sender: Any) { 
     let parameters = ["acceptPrivacyNotice": true, "name" :"xxxxx xxxxx", "email": "[email protected]", "password":"Qwerty2012", "passwordConfirm":"Qwerty2012","deviceID" : "", "isProvider" : false, "idTypeProvider": 1] as [String : Any] 

     guard let url = URL(string: "https://www.apps-sellcom-dev.com/Engie/api/account/register") else {return} 
     var request = URLRequest(url: url) 
     request.httpMethod = "POST" 
     request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
     request.addValue("M1o2K1RVMzRHVSNteUtLOjNzSCR5LUEyKk5qOEhFRg==", forHTTPHeaderField: "Authorization") 

     guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { 
      return 
     } 
     request.httpBody = httpBody 

     let session = URLSession.shared 
     session.dataTask(with: request) { (data, response, error) in 
      if let response = response { 
       print("Response",response) 
      } 
      if let data = data { 
       do { 
        let json = try JSONSerialization.jsonObject(with: data, options: []) 

        print(json) 
       } catch { 
        print(error) 
       } 
      } 
     }.resume() 
    } 
+0

Verwenden Sie 'JSONSerialization' nicht, vielleicht bearbeitet Ihr Server dieses Format nicht, versuchen Sie es mit der normalen Zeichenkette zuerst zB' key = value & ... ' – Tj3n

Antwort

0

Try this:

@IBAction func buton(_ sender: Any){ 
    let params = ["acceptPrivacyNotice": true, "name" :"xxxxx xxxxx", "email": "[email protected]", "password":"Qwerty2012", "passwordConfirm":"Qwerty2012","deviceID" : "", "isProvider" : false, "idTypeProvider": 1] as [String : Any] 
     let session = Foundation.URLSession.shared 
     let url = URL(string: "https://www.apps-sellcom-dev.com/Engie/api/account/register") 
     var request = URLRequest(url : url!) 
     request.httpMethod = "POST" 


     do { 

      let jsonData = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) 
      request.addValue("M1o2K1RVMzRHVSNteUtLOjNzSCR5LUEyKk5qOEhFRg==", forHTTPHeaderField: "Authorization") 
      request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") 
      request.httpBody = jsonData 

      session.dataTask(with: request, completionHandler: { data, response, error in 
       OperationQueue.main.addOperation { 

        guard error == nil && data != nil else { 
         print("error=\(String(describing: error))") 
         return 
        } 

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   // check for http errors 
         print("statusCode should be 200, but is \(httpStatus.statusCode)") 
         print("response = \(String(describing: response))") 
        } 

        let responseString = String(data: data!, encoding: String.Encoding.utf8) 
        print("responseString = \(responseString!)") 

        if let responsedata = responseString!.data(using: String.Encoding.utf8)! as? Data{ 

         do { 

          let jsonResult:NSDictionary = try JSONSerialization.jsonObject(with: responsedata, options: []) as! NSDictionary 
          print("Get The Result \(jsonResult)") 
          if error != nil { 
            print("error=\(String(describing: error))") 

          } 


          if let str = jsonResult["success"] as? NSNull { 
           print("error=\(str)") 

          } 
          else { 
           let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
           print("Response string : \(String(describing: responseString))") 
          } 



         } catch let error as NSError { 
          print(error.localizedDescription) 
         } 
        } 
       } 
      }) .resume() 
     }catch { 

     } 

    } 
0

ich Ihren Code und den Grund, warum Sie nicht eine Antwort sehen getestet habe, ist, dass der Abschluss-Block nicht tut alles im Falle eines Fehlers.

Wenn ich Ihre Anfrage lief, kam es mit dem folgenden Fehler zurück

Error Domain=NSPOSIXErrorDomain Code=100 "Protocol error" UserInfo={NSErrorPeerAddressKey=<CFData 0x608000092200 [0x101840c70]>{length = 16, capacity = 16, bytes = 0x100201bb34bface50000000000000000}, _kCFStreamErrorCodeKey=100, _kCFStreamErrorDomainKey=1} 

Meine beste Vermutung ist, dass es etwas falsch in der httpBody ist. Ich hoffe, das hilft.

+0

Ok, vielen Dank Ich werde immer wieder überprüfen, dass ich denke, dass es weil Der Parameter "deviceId" im Postman sollte Null sein und ich habe keine Erfahrung mit diesen Datentypen. – user8172235