How to retrieve Alamofire response header for a request
If the response is type of NSHTTPURLResponse
you can get header from response.allHeaderFields
.
So when you use Alamofire responseJSON you can access to NSHTTPURLResponse property like this :
Alamofire.request(.GET, requestUrl, parameters:parameters, headers: headers).responseJSON {
response in
print(response.response?.allHeaderFields)
}
As apple documentation says :
A dictionary containing all the HTTP header fields received as part of the server’s response. By examining this dictionary clients can see the “raw” header information returned by the HTTP server.
The keys in this dictionary are the header field names, as received from the server. See RFC 2616 for a list of commonly used HTTP header fields.
So to get for example a content-type in response header you can access it in that way :
if let contentType = response.response?.allHeaderFields["Content-Type"] as? String {
// use contentType here
}
How to get response headers when using Alamofire in Swift?
As response is of NSHTTPURLResponse
type, you should be able to get the headers as followed:
response.allHeaderFields
Alamofire read header response
You can use allHeaderFields
property of HTTPURLResponse
for that.
print(response.response.allHeaderFields["X-Page-Max-Index"])
Not able to retrieve the exact queried response using Alamofire
I think something is going on with Alamofire package after archiving the project. I tried recreating the project and it worked fine until I archived the project to test on another device. So, I've switched over to urlRequest instead of using Alamofire request. Replacing Alamofire request with the below worked fine for me.
var urlRequest = URLRequest(url: URL(string: "https://mywesite.com/wp-json/wp/v2/posts?per_page=100")!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
Alamofire POST Request with request header & body
You are not passing grant_type
parameter. Try changing your params
dictionary to this:
let params = [
"client_id": "123",
"grant_type": "refresh_token",
"refresh_token": "123"
]
How do I see Alamofire Request?
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.
Alamofire get method with parameters in the url with header swift
You user parameters like url and it's wrong way to make request like this.
You need add parameters on request method like parameters. And yes, you can use parameters on 'GET' requests.
let header: HTTPHeaders = ["Content-Type":"application/json","x-token":self.token!]
let todosEndpoint: String = "https://reachwebdemo.com/2020/10/listcribdev/api/chatnotification"
let params:[String: Any] = ["channel_sid":self.channelsid!, "author":self.userid!, "message": inputMessage]
if let encoded = todosEndpoint.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
let url = URL(string: encoded) {
print("notify url is",url)
AF.request(url, method: .get, parameters: params, encoding: URLEncoding.default, headers: header).responseString { response in
switch response.result {
case .success(let json):
print("Validation Successful for push notification",json)
case let .failure(error):
print("error for push notificaton",error.errorDescription)
}
}
}
Related Topics
What Does the '@' Symbol Mean in Swift
How to Calculate the 21! (21 Factorial) in Swift
Reference as Key in Swift Dictionary
Swift 4.2 Imagepickercontroller Issue
Viewcontroller Slide Animation
How to Reference Swift Playground Itself
When and How to Use @Noreturn Attribute in Swift
Swift 2 Protocol Extension Not Calling Overridden Method Correctly
Draw a Hole in a Rectangle with Spritekit
"Unrecognized Selector Sent to Instance" in Swift
Is There a Writetofile Equivalent for Swift 3's 'Data' Type
Swift, How to Play Sound When Press a Button
How to Declare an Inline Function in Swift
Set a Default Value for Uipickerview in Swift