Mock your APIs without any code change with Charles Proxy

Prateek Srivastava
Jan 21 · 3 min read
Using Charles Proxy with Mocklets
Using Charles Proxy with Mocklets

If you have ever developed any front-end application whether web or mobile, its very likely you must have heard about Charles Proxy. But for those who haven’t, Charles is, as described on its site:

Charles is an HTTP proxy / HTTP monitor / Reverse Proxy that enables a developer to view all of the HTTP and SSL / HTTPS traffic between their machine and the Internet. This includes requests, responses and the HTTP headers (which contain the cookies and caching information).

Now, in the last article, we learned about how Mocklets can be used to speed up the development process. But speed only doesn’t always suffice the needs of a better development process. We need a quality product too. This is where Mocklets comes to rescue, again.

Quality assurance is a critical part of application development. Mocklets empowers the quality testing by allowing testing of edges cases and inducing production environment which actual servers cannot reproduce reliably. But since quality testing will be taking place on your release candidate build, it is very late to keep making changes in your code to point API requests to Mocklets.

So how do we point some of our API requests to Mocklets, while keep pointing to actual server APIs, without making any code changes.

Proxying to Mocklets with Charles

Before moving on to this section, we need to setup Charles Proxy to intercept the API calls made from your app. There is a nice article here, about setting up Charles Proxy.
If you already know how to setup Charles, you are good to proceed.

Now that Charles Proxy is setup, it will start showing all the calls being made from our application.

Let’s take an example where our application is making a GET request to http://dummy.restapiexample.com/api/v1/employees which responds with the list of employees.

Image for post
Image for post
Response from server API

Now, let’s try to forward requests made to above API to Mocklets. For that we first need to create a mock API on Mocklets.

Image for post
Image for post

Now, we will map our API request, going to our server, to be forwarded to Mocklets API. (Right click on the specific request > Select “Map Remote”)

Image for post
Image for post

Once you see Edit Mapping dialog, provide the details required to forward the request and press OK.

And, that’s all, you are all done. From now onwards, all the requests you make to the specified API, those will be forwarded to Mocklets API and you will receive a mock response.

Image for post
Image for post
Response from Mocklets API

Isn’t it super easy!!!

Using Mocklets with Charles Proxy, gives you flexibility to send some requests to Mocklets, while keep pointing to actual server for rest, that too without making any changes in application code. This ability make Mocklets indispensable to application development process.

Try out Mocklets, and if you need more information about how to use this tools, visit their documentation page.

Stay tuned for next article

Mocklets also supports file hosting, which can be used to share your assets as well as use them in your mock APIs. Stay tuned for the next article where I will discuss more about their file hosting feature.

Happy coding!!!

Mocklets

Make better apps faster by mocking the APIs you depend on

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store