2016-11-30 16 views
1

Ich versuche, Daten zu veröffentlichen POST Anfrage, aber wenn ich Daten posten bekomme ich Bad Request Fehler.JSON POST Parsing Problem in Swift 3

Bitte diesen Code überprüfen und, wenn es irgendein Problem bitte mich wissen lassen,

func callDataCall() 
{ 
    let requestURL: NSURL = NSURL(string: "REQUEST_URL")! 

    //convert MID dict to jsondata 
    let NewJSONData = try! JSONSerialization.data(withJSONObject: ["Disease_request":["Mid":self.MedicationID]], options: []) 

    // Convert jsondata to string 
    let NewJSONDataString = NSString(data:NewJSONData, encoding:String.Encoding.utf8.rawValue) as! String 

    print("Created Dictionary is : \(NewJSONDataString)") 

    let urlRequest = NSMutableURLRequest(url: requestURL as URL) 

    // set up the session 
    urlRequest.httpMethod = "POST" 

    urlRequest.httpBody = NewJSONDataString.data(using: String.Encoding.utf8) 

    let task = URLSession.shared.dataTask(with: urlRequest as URLRequest) 
    { 
     data, response, error in 

     let httpResponse = response as! HTTPURLResponse 
     let statusCode = httpResponse.statusCode 

     print("status code is :\(httpResponse)") 

     if (statusCode == 200 || statusCode == 201) 
     { 
      do 
      { 
       let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject] 

       if let content = json?["Disease"] as? [[String: AnyObject]] 
       { 
        for i in 0..<content.count 
        { 
         self.medicationContent.append(content[i] as AnyObject) 
        } 
        DispatchQueue.main.async(execute: { 
         self.tableview_Medication.reloadData() 
        }) 
       } 
      } 

      catch 
      { 
       print("Error with Json: \(error)") 
      } 

      self.stopIndicator() 
     } 
    } 
    task.resume() 
} 

Antwort ist:

<NSHTTPURLResponse: 0x608000231e40> { URL: "REQUEST_URL" } { status code: 400, headers { 
    "Cache-Control" = "no-cache"; 
    "Content-Length" = 46; 
    "Content-Type" = "application/json; charset=utf-8"; 
    Date = "Wed, 30 Nov 2016 10:38:24 GMT"; 
    Expires = "-1"; 
    Pragma = "no-cache"; 
    Server = "Microsoft-IIS/8.5"; 
    "X-AspNet-Version" = "4.0.30319"; 
    "X-Powered-By" = "ASP.NET"; 
    "X-Powered-By-Plesk" = PleskWin; 
} } 

ich für Objective C die gleiche Logik verwende, seine Arbeit für mich in Ordnung .

+0

'lassen reqJSONString = "\ ("{\" Disease_request So nach Httpmethod

eine weitere Zeile hinzufügen \ ":") \ (JSONDataString) \ ("}") "' müssen Sie JSONString wirklich in JSON einbetten? Außerdem müssen Sie möglicherweise die Länge in der Kopfzeile festlegen. – Larme

+0

tatsächlich Inhalte einstellen sollte wie so sein: { "Disease_request": { "Mid": "37" }} – Piyush

+1

Bitte Refactoring Code Swift native Typen zu verwenden ('URL',' URLRequest', 'String' , '[String: Any]') und das Standard-JSON-Wörterbuch ist '[String: Any]' in Swift 3. Und vergessen Sie 'PrettyPrinted'. Der Server kümmert sich überhaupt nicht. – vadian

Antwort

0

Die Lösung erhalten !!

Der Swift-Code ist in Ordnung, ich brauche jetzt der Code als ähnliche

urlRequest.httpMethod = "POST"

urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")