2017-04-08 106 views
0

我想,包括我的快捷應用程序內Zoho的圖書API ZOHO的書籍POST方法。 我想創建一個項目使用POST方法,但我不斷有一個錯誤消息說我的JSONString是錯誤的。中使用JSON的斯威夫特3

這裏是ZOHO文檔發送請求,使用POST方法,以創造和項目:

https://books.zoho.com/api/v3/items 
    ?authtoken=e07119171812c29b3a0dacdb79a57e3f 
    &organization_id=10234695 
    &JSONString={ 
     "name": "Hard Drive", 
     "description": "500GB, USB 2.0 interface 1400 rpm, protective hard case.", 
     "rate": 120.00, 
     "account_id": "460000000000388", 
     "tax_id": "460000000027005" 
} 

這是我做。我感到莫名其妙我JSONString犯規達到預期格式...

let postData = [ 
      "authtoken" : "ZZZZZZZZZZZZZZZZZZZZZZ", 
      "organization_id" : "XXXXXXXXXXX", 
      "JSONString" : 
       [ 
       "name": "Hard Drive", 
       "description": "500GB, USB 2.0 interface 1400 rpm, protective hard case.", 
       "rate": 120.00, 
       "account_id": "460000000000388", 
       "tax_id": "460000000027005" 
       ] 
      ] as [String : Any] 

     var request = URLRequest(url: NSURL(string:"https://books.zoho.eu/api/v3/items")! as URL) 
     request.httpMethod = "POST" 
     request.httpBody = try! JSONSerialization.data(withJSONObject: postData, options: []) 
     request.addValue("application/json", forHTTPHeaderField: "Content-Type") 

     Alamofire.request(request).responseJSON 
      { response in 
       if let JSON = response.result.value { 
        let zohoResponse: [String: AnyObject] = JSON as! [String : AnyObject] 
        print(zohoResponse) 
       } 
     } 

而答案:

["code": 4, "message": Invalid value passed for JSONString] 

我管好,使GET請求,所以我認爲它應該是管理使用中的POST方法我的快速應用程序也是如此。

非常感謝您的幫助

問候

回答

0

UPDATE:

我找到的解決方案是通過發送使用字符串參數,而不是JSON一個POST請求。

如果你有一個替代的解決方案,我interrested,但我使用的一個作品......

class func sendPostZoho(url : String, dataArray : [String: AnyObject] , completionHandler : @escaping ([String : AnyObject])->()) { 
    let jsonstring = try! JSONSerialization.data(withJSONObject: dataArray, options: []) 
    let convertedString = String(data: jsonstring, encoding: .utf8) 

    let percentageString = convertedString?.replacingOccurrences(of: "\"", with: "%22") 
    var request3 = URLRequest(url: URL(string: url)!) 
    request3.httpMethod = "POST" 
    let StringtoPost = "&JSONString=" + percentageString! 

    request3.httpBody = StringtoPost.data(using: .utf8) 

    Alamofire.request(request3).responseJSON 
     { response in 

      if let JSON = response.result.value { 
       let DictionnaryResponse: [String: AnyObject] = JSON as! [String : AnyObject] 
       completionHandler(DictionnaryResponse) 
      } 
    } 
}