【问题标题】:POST method for zoho books using Json for Swift 3使用 Json for Swift 3 的 zoho 书籍的 POST 方法
【发布时间】:2017-04-08 20:16:32
【问题描述】:

我正在尝试在我的 swift 应用程序中包含 Zoho Books API。 我想使用 POST 方法创建一个项目,但我一直收到一条错误消息,提示我的 JSONString 错误。

这是使用 POST 方法发送的 zoho 文档请求,以便创建和项目:

 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 请求,所以我相信在我的 swift 应用程序中使用 POST 方法也应该是可以管理的。

非常感谢您的帮助

问候

【问题讨论】:

    标签: json swift post zoho


    【解决方案1】:

    更新:

    我找到的解决方案是使用字符串参数而不是 JSON 参数发送 post 请求。

    如果您有替代解决方案,我很感兴趣,但我使用的解决方案有效......

    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)
                }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多