0

I am trying to use Alamofire for an Api that has a Json body and a header requirement that needs Basic Authentication. When I test the call in Postman it works fine, however with AlamoFire I am getting a server error.

I am trying to see what the call is that's going out from Alamofire - but I can't seem to see it: (this is the code I am using)

edit:

Error is below

["errors": { }, "errorMessages": <__NSSingleObjectArrayI 0x6000000035c0>( Internal server error ) ]

Request is below

  Alamofire.request(endpoint, method: .post, parameters: paramsEncoded, encoding: JSONEncoding.default, headers: headers)
        .responseJSON(completionHandler: { (response) in
            print(response.request) // This returns just the url eg "http://api.com
            print(response.request?.httpBody) // This returns `Optional(85 bytes)`
        })

I assume the request is not being created properly, but would be useful to understand what I should be seeing.

Edit: this line helped from the other question : NSLog("Request: \(request!.httpMethod!) - \(request!.url!.absoluteString)\n\(request!.httpBody.map { body in String(data: body, encoding: .utf8) ?? "" } ?? "")")

Thanks to the question I have linked to as a duplicate for the help.

UKDataGeek
  • 6,338
  • 9
  • 46
  • 63

2 Answers2

6

Print the whole request like this :

let request = Alamofire.request(endpoint, method: .post, parameters: paramsEncoded, encoding: JSONEncoding.default, headers: headers)
    .responseJSON(completionHandler: { (response) in
        print(response.request) 
        print(response.request?.httpBody)
    })

print("REQUEST = \(request)")

I'm not sure you can get any more info than that though.

AMAN77
  • 6,218
  • 9
  • 45
  • 60
-1

You need to add headers with your request.

let urlString = "https://httpbin.org/get"
let headers = ["Authorization": "123456"]

Alamofire.request(urlString, method: .post, parameters: ["foo": "bar"],encoding: JSONEncoding.default, headers: headers).responseJSON { response in
    print(response.result.value)
}
Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51