0

Im fairly new to iOS development and I'm working on a project that requires integration with Active Campaign. I simply want to make a post request using their API to add a contact to the database when a button is pressed.

I've first attempted a get request for the current contacts using Alamofire to make sure I can get to it and I can't seem to get the desired output. I've searched and googled quite a bit to find a solution. I've found a few different ways to do it online but nothing has seemed to work.

When I run my current code to add a contact I get this error:

responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))

Below is my code to add a contact. I will say this is after trying many different solutions I've come across and I may have confused myself. Any help is much appreciated!!

    import UIKit
    import Alamofire
    import SwiftyJSON


    class emailEntry: UIViewController {

    @IBOutlet weak var f_name: UITextField!
    @IBOutlet weak var l_name: UITextField!
    @IBOutlet weak var email: UITextField!
    @IBOutlet weak var center_frame: UIView!

    var api_key = "MY_KEY_IS_HERE"
    let urlBase = "BASE_URL_HERE"


    @IBAction func submit_but(_ sender: UIButton) {


        let url = "\(urlBase)/admin/api.php?api_action=contact_add"

        guard let authHeader = Request.authorizationHeader(user: "AnyString", password: api_key) else{
            print("Nothing")
            return
        }

        let parameters:Parameters = ["first_name":f_name,
                                     "last_name":"l_name",
                                     "email":"email"
                                    ]
        let headers:HTTPHeaders = [authHeader.key: authHeader.value]


        Alamofire.request(url, method: .post , parameters: parameters, headers: headers).validate().responseJSON { response in
                switch response.result {
                case .success:
                    print(response)

                    break
                case .failure(let error):

                    print(error)
                }
        }
    }
}
tpos17
  • 1

1 Answers1

0

That error indicates the response you got back was not JSON. Most likely it was some sort of HTML error page. Check the API docs and make sure you're properly formatting your request. Also, you can use responseString to see the content of the response in plain text.

Jon Shier
  • 12,200
  • 3
  • 35
  • 37
  • Hi @Jon, Thank you for the response. When I use responseString it tells me "You are not authorized to access this file". I know the Api Key is correct. Can you tell me if I am passing the key incorrectly? I am a bit stuck on this at the moment. Thanks! – tpos17 Dec 06 '18 at 16:00
  • Hard to say. Does the API expect the key as an authorization header with a bearer token? That's a special encoding, so make sure it's what you need. – Jon Shier Dec 07 '18 at 04:08