Zhongde Liu

This is my personal "Clipboard" of web development stuff I come across

Leave a comment

Standards/Guidelines for building an HTTP API/Webservices/REST

General Overview about HTTP API Design

Thoughts on RESTful API Design

A document describing the experience the author, Geert Jansen, gained from designing the RESTful API for Red Hat’s Enterprise Virtualization product.  Summarizes his thoughts about how a “good” REST Service should look.


HTTP API Design Guide from Heroku

The Heroku team has published their HTTP API Design Guide. It is a short guide / best-practice collection, giving a nice overview which aspects you should consider when designing an API.


Data formats

Following two links describing how the response should be formatted: These are consistent, documented quasi-standards for use as response formats. Main topic is the linking and discoverability between resources.

JSON API :: A standard for building APIs in JSON.

Living document, written by Steve Klabnik and Yehuda Katz. Media-type: application/vnd.api+json


JSON HAL – Hypertext Application Language

Mike Kelly wrote down a convention expressing hyperlinks in JSON (or XML) and named it HAL. Media-type: application/hal+json

http://stateless.co/hal_specification.html,  http://tools.ietf.org/html/draft-kelly-json-hal-06

Collection+JSON – Hypermedia Type

A format definition focused on collections. Media-Type: application/vnd.collection+json




Create/start a simple local HTTP Server

In order to develop or test some static code, without copying them into your apache webroot or something else, you easily can start a simple static webserver within the directory you are currently in with following tools.
Because you sometimes you just need a http:// (likely localhost) Url instead of a file://… one.

  1. If you have Python (https://www.python.org/) installed you can start a static web server with following command, depending on the installed version.
    python -m SimpleHTTPServer [port]//python2
    python3 -m http.server [port]//python3
  2. If you have PHP > 5.4.0 (http://php.net/) installed you can start one with:
    php -S localhost:[port]
  3. Using Ruby (https://www.ruby-lang.org/) you setup a simple server with this command:
    ruby -run -e httpd . -p [port]
  4. For node.js (http://nodejs.org/) I use the  “simple, zero-configuration command-line http server” package (https://www.npmjs.org/package/http-server).
    After the default npm installation

    npm install http-server -g

    you just type

    http-server -p[port]

    into your console.

These are some nifty tools (or “oneliners“), when it comes to browse a static/client app/website/file with your browser in your local environment without copying the files somewhere else.

Leave a comment

Code: “cast a number to int” with bitwise operators

In order to cast a number to an int value you can also use bit operations. In particular the Double NOT and the OR 0.

~~(3.14) // => 3
3.14 | 0 // => 3

As these operations are working on on signed 32-bit integers per default, the number is in fact truncated after the comma. This also works on negative numbers.

~~(-3.14) // => -3
-3.14 | 0 // => -3

You are asking Why? Why should anyone bother bitwise operators? one word: Performace. If you are crunching a lot, I mean, a lot, numbers, you should notice something. I made a quick jsperf http://jsperf.com/parseint-and-bitwise-operations. You see the bit operations are more than 10x faster than parseInt.

Sidenote: Math.floor is also pretty fast. However Math.floor results in a different result number when used with negative ones.

Math.floor(-3.14) // => -4

Leave a comment

How-to: Proxy setting for “Github for Windows”

For setting the proxy you need to edit a file named .gitconfig. This file should be located in your user folder, C:\Users\[xxxxx]\.gitconfig with [xxxxx] being your windows username.

In this file you need to add following lines, adapted to your proxy:

proxy = http://proxy.example.com:8080 

proxy = http://proxy.example.com:8080

Leave a comment

Code: load handler with cached images and IE

Just had a strange case, that the load event of an image is not fired. After a short research I found the issue…

Not working code:
$('img').attr('src', 'image.jpg').load(function() {
//Do something - but it doesn't

Working code:
$('img').load(function() {
//Do something - it does
}).attr('src', 'image.jpg');

The thing is IE (and Opera) will load the cached image directly before your event handler is attached…

It is jQuery Notation but also applies to Vanilla JS.


How-To: npm behind a proxy

When you want to use npm (http://npmjs.org/), package manager for node, behind a proxy, pay attention that there are two parameters for setting up a proxy:

  1. proxy
  2. https-proxy

As https is the default protocol for fetching the packages from the npm repository you must set the second one.

Another workaround is to change the rep url into a http one.

Leave a comment

Allowing HTML in WordPress user profile description

One of my latest tasks was to create a fancy author page in a WordPress powered blog. So I thought, let the authors fill out there description field in the admin backend and pull the data out for display on the author page. To put themselves under the right light some of the authors used hmtl-formatted texts to describe them. Good idea!

Unfortunately WordPress has a filter, which strips nearly all of the html-tags used in the description text. So the result was pretty ugly ….

After some research, how others solve this issue, I was shocked that hacking the core – tipps were the top google hits (e.g. here or here) . Just an advice independent from this issue. “Don’t hack the core !” You maybe will regret this later ….

As I mentioned there is a filter which strips out the html-tags. So there is the handy function called remove_filter() in wordpress. You just have to identify the one you have to remove…

All in all here is the solution instead of hacking the core, just add this line somewhere in your php code (preferably functions.php):

remove_filter('pre_user_description', 'wp_filter_kses');

And there you go, no html is stripped from the input text. But be careful, you should trust your users. Because this filter has a reason 😉