Simulating 500 HTTP status code in Create Tweet API on Twitter

Nobody wants to see errors in their API. But they appear at times when least expected. The possible scenarios when an error pops up could be something is wrong with the request or some problem with the API. Whatever may be the cause, one thing is sure traffic would stop.

API crashes shouldn’t disrupt the user experience. Errors should be handled at the client-side so that the user knows that something is wrong and can move on from there.

In the development phase, API consumers won’t wait until the API is entirely built to start their development. In this scenario, they can mock server responses and begin their development. Testing the application for error responses is crucial so that user traffic is not blocked.

A QA tester or Product manager would want to mock various error responses to check how the application handles the errors.

How to Simulate API error responses using Requestly

Requestly is a tool that intercepts all the network requests and allows users to modify them. It enables users to Modify Headers, Redirect URLs, Switch hosts, Mock API responses, Delay network requests, Insert custom scripts. It is available as a desktop app and browser extension on all major browsers.

Requestly app can simulate status code responses by using the redirect rule to redirect to a mock API that overrides the status code. In simple words, you are hitting the mock API instead of the actual API.

Mock Server

I used it to simulate status code responses on Twitter

I tried this with the following sites to see how the site handles the simulation of 500- Internal Server Error status code.

Simulating 500 status code when Tweeting

I created a mock API using requestly setting Status Code as 500.

Mock for simulating 500 error response on Twitter Mock for simulating 500 error response on Twitter

After some reverse engineering, I noticed that when the tweet button is clicked, a POST request is sent to a Twitter API ending with CreateTweet.

So I created the Requestly rule to be applied only when the URL matches */CreateTweetusing wildcard match.

Redirect Rule redirecting to mock API Redirect Rule redirecting to mock API

The redirection to mock API won’t work due to Content-Security-Policy- connect-src directive, which restricts the URLs loaded using script interfaces.

You can use the Requestly marketplace rule to bypass this, which is a prebuilt rule to remove the content-security-policy header.

Requestly Marketplace

When I tried tweeting something, 500 status code was simulated and handled as following.

500 error response simulated when tweeting 500 error response simulated when tweeting

Get the rule here- https://app.requestly.io/rules/#sharedList/1640613384657-twitter-500-simulate

This is how I used Requestly to simulate 500 when tweeting. I hope this article has helped you and empowers you to simulate different status codes.

Happy Debugging!

Further Resources:

By @nafees87n

Get to know Requestly

Become a 10x developer by using Requestly in your development & testing workflows.

No spam guaranteed :)