Search code examples
javascripthttpionic2emoji

send emoji in http header ionic2


I am not able to send emoji in HTTP header though emoji is a plain text.
Error is emoji is not a valid HTTP header field value.

createStatus(user: any, status: any) {
    let that = this;
    let headers = new Headers();
    headers.append('Content-type', 'application/json');
    headers.set('token', user.accessToken);
    headers.set('facebookId', user.facebookId);
    headers.set('operation', 'create');
    headers.set('status', status);

    return that.http.post(Constants.API_URL + '/post', {}, {headers: headers})
      .toPromise()
      .then(response => response.json())
      .catch(that.handleError);
  }

Please tell me what I am doing wrong.


Solution

  • For HTTP header value, only ASCII characters are guaranteed to work. Other characters, such as Emoji or Chinese characters won't be allowed by most HTTP client. Please refer to What character encoding should I use for a HTTP header? for some previous discussion.

    To send Emoji in HTTP request, there are 2 methods:

    1. Encode Emoji character and send it as header. For example:

      var status = encodeURIComponent('⌛'); // %E2%8C%9B
      headers.set('status', status);
      

      When the request is received in server, decode it:

      var statusValEncoded = '%E2%8C%9B';
      var statusVal = decodeURIComponent(statusValEncoded); // ⌛
      
    2. Send Emoji character as request body. For example:

      that.http.post(Constants.API_URL + '/post', {status: '⌛'}, {headers: headers})