2

I am attempting to interact with an API. The service usually works and returns a 200 response but will occasionally respond with a 400 error and only a blank headers in the debug description. I have made a recursive function to attempt to pass the same request and I end up with a 200 response within 1-3 tries even though I am making the same request. I have made requests through Postman and PHP and never experience this issue when interacting with the API. Only when making requests in Swift.

    func process(table: String, path: String, objectId: String? = nil, pendingWriteId: String? = nil, method: ApiMethod, processData: [String: Any] = [:], filter: [String: Any] = [:], initializing: Bool = false, remove: Bool = false, completion: @escaping (Error?, JSON) -> Void) {
                var timestamp: Int = Int(Date().timeIntervalSince1970)

                let session = URLSession.shared
                        let url : String = "\(apiURL)\(path)"

                    var params: [String:Any] = [:]

                    if method != .GET {
                        params["data"] = processData
                        params["filter"] = filter
                        params["timestamp"] = timestamp
                    }

                    if method == .DELETE {
                        params["remove"] = remove
                    }

                    let request = NSMutableURLRequest(url: NSURL(string: url)! as URL)
                    request.httpMethod = method.rawValue

                    if let token = auth.authToken {
                        request.setValue(token, forHTTPHeaderField: "token")
                    }

                    print(url)
                    self.sendRequest(session: session, request: request, params: params, pendingWriteId: pendingWriteId, initializing: initializing) { (error, json) in
                        completion(error, json)
                        return
                    }
        }

        func sendRequest(session: URLSession, request: NSMutableURLRequest, params: [String:Any], pendingWriteId: String?, initializing: Bool, completion: @escaping (Error?, JSON) -> Void) {
            do{
                request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [JSONSerialization.WritingOptions.prettyPrinted])

                _ = session.dataTask(with: request as URLRequest as URLRequest, completionHandler: {(data, response, error) in
                    DispatchQueue.main.async {
                        if let response = response {
                            let nsHTTPResponse = response as! HTTPURLResponse
                            let statusCode = nsHTTPResponse.statusCode
                            if statusCode == 200 {
                                if let d = data {
                                    do {
                                        let json = try JSON(data: d)
                                        print(json)
                                        completion(error, json)
                                        return
                                    } catch {
                                        completion(error, JSON())
                                        return
                                    }
                                }
                            } else {
                                print ("\(request.url) status code = \(statusCode)")
                                print(nsHTTPResponse.description)
                                self.sendRequest(session: session, request: request, params: params, pendingWriteId: pendingWriteId, initializing: initializing) { (error, json) in
                                    completion(error, json)
                                    return
                                }
                            }
                        } else {
                            completion(error, JSON())
                            return
                        }
                    }
                }).resume()
            }catch _ {
                print ("Oops something happened")
                completion(ApiError.willNotResolve, JSON())
                return
            }
        }
Sean Cook
  • 435
  • 7
  • 21

1 Answers1

3

After nearly a day of trying to figure this out I solved the issue in a matter of minutes of posting the question. The issue was that I was attempting to JSON encode an empty parameter set. I changed

request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [JSONSerialization.WritingOptions.prettyPrinted])

to

if params.count > 0 {
       request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [JSONSerialization.WritingOptions.prettyPrinted])
}

and it now works as expected

Sean Cook
  • 435
  • 7
  • 21