Search code examples
swiftafnetworking

Error:Request failed: bad request (400) afnetworking batch in swift


I am trying to get batch to push notifications to my app through swift. I am trying to convert the curl command in this site below through swift, so that i can request a push notification from batch through swift:

https://batch.com/doc/api/transactional.html

Here is my attempt in swift:

let manager = AFHTTPRequestOperationManager()
            manager.requestSerializer.setValue("REST API KEY", forHTTPHeaderField: "X-Authorization")
            manager.requestSerializer.setValue("application/json", forHTTPHeaderField: "Content-Type")

        var param: [String:AnyObject] = [
                "group_id": "welcome",
                "recipients": [
                    "tokens": ["USER_TOKEN"]
                ],
                "message": [
                    "title": "Hello",
                    "body": "Hi!"
                ],
                "custom_payload": "{\"tag\":\"wake up push\", \"landing_screen\":\"greeting\"}",
                "sandbox":true
        ]

        manager.POST( "https://api.batch.com/1.0/"API KEY"/transactional/send",
        parameters: param,
        success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
        print("Push Notification Successfully sent!")
        print("JSON:" + responseObject.description)
        },
        failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
        print("Error:" + error.localizedDescription)
        })

I kept on getting error:

Error:Request failed: bad request (400)

Any Ideas/Directions will be welcomed Thanks!


Solution

  • You should better use this Swift client for the Batch.com Transactional API.

    All HTTP response error codes for this API are explained in the documentation but a 400 Bad request error shouldn't happening, it must be a temporary error on Batch.com's end.

    I suppose you have replaced REST API KEY, USER_TOKEN and API KEY with valid values?