DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Five Minute Swift: Debugging Alamofire Requests

Five Minute Swift: Debugging Alamofire Requests

Learn how to use the power of extensions to see what how your HTTP requests are formed with Alamofire.

James Sugrue user avatar by
James Sugrue
CORE ·
Feb. 14, 17 · Code Snippet
Like (4)
Save
Tweet
Share
15.22K Views

Join the DZone community and get the full member experience.

Join For Free

When REST API calls go wrong, it can be really frustrating to work out where the problem is. Are you missing a header? Or have you sent something incorrect in your request?

There's no shortage of debugging methods for this. There are traditional approaches like using Wireshark to debug all requests and responses, or by installing Charles as an HTTP Monitor on your machine. More recently, there have been some in-app debugging solutions like ResponseDetective which intercepts all calls made by the URLSession. Dotzu gives you a view into networking info, among other logs, so that you can view all calls made by your app.

However, there are times that you just need a really simple solution, without adding yet another framework to your app. If you're already using Alamofire, by leveraging the power of extensions, you'll be able to easy view outgoing requests. 

First, you'll need to define an extension to the Request class as follows:

extension Request {
    public func debugLog() -> Self {
        #if DEBUG
            debugPrint("=======================================")
            debugPrint(self)
            debugPrint("=======================================")
        #endif
        return self
    }
}


This extension will only run as you are debugging the application and prints out the entire Request to the console. 

To use the extension, just use debugLog() after defining your request, like so: 

  Alamofire.request(url).debugLog()
            .responseJSON( completionHandler: { response in
   })


Requests Swift (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Last Chance To Take the DZone 2023 DevOps Survey and Win $250! [Closes on 1/25 at 8 AM]
  • Load Balancing Pattern
  • A Real-Time Supply Chain Control Tower Powered by Kafka
  • Kotlin Is More Fun Than Java And This Is a Big Deal

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: