A consideration on URL encoding when posting a tweet on the Twitter platform
This essay shows you how to have URL encoded tweets when using the Twitter API.
When Twitter API user agents or clients request for a tweet to be posted on the Twitter platform, the Twitter API documentation specifies that tweets should be “URL encoded as necessary.”
If you send an API request to post a tweet without accounting for the URL encoding that the Twitter platform will perform on your proposed tweet, then it’s possible that the proposed tweet will become elongated and thus exceed the 280 character limit. That would cause the Twitter API server to send an API error in response, namely specifying that the proposed tweet exceeds the 280 character limit. To resolve this, we’ll need an algorithm for solving the problem of maximizing the number of characters we can request the Twitter platform API server to accept, which is the object of this discussion.
We consider the following algorithm:
- Encode the original tweet message string to be a URI encoded string.
- Extract the first 280 characters of the new URI encoded string.
- Using the shortened form of the encoded string, decode it from an encoded URI string into a non-encoded URI string.
To encode the string, we use the
substr string method to extract the first 280 characters of the encoded string. Then, to decode the encoded string, the
const URIEncodedString = encodeURIComponent(original_tweet_message_string); const shortenedURIEncodedString = URIEncodedString.substr(0, 280); const decodedShortenedURIEncodedString = decodeURIComponent(shortenedURIEncodedString);
original_tweet_message_string variable is the tweet status string that is being requested to be posted.
Following the algorithm’s execution, we now have a string that can confidently be sent to the Twitter API server. The server will perform its encoding on the string and the resulting proposed tweet status string will not exceed the 280 character restriction due to the preprocessing we performed.