在 http header ionic2 中发送表情符号
send emoji in http header ionic2
尽管表情符号是纯文本,但我无法通过 HTTP header 发送表情符号。
错误是表情符号不是有效的 HTTP header 字段值。
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);
}
请告诉我哪里做错了。
对于 HTTP header 值,只有 ASCII 字符可以保证有效。大多数 HTTP 客户端不允许使用其他字符,例如表情符号或中文字符。之前的一些讨论请参考What character encoding should I use for a HTTP header?。
在HTTP请求中发送Emoji,有2种方法:
编码表情符号字符并将其作为 header 发送。例如:
var status = encodeURIComponent('⌛'); // %E2%8C%9B
headers.set('status', status);
当服务器收到请求后,对其进行解码:
var statusValEncoded = '%E2%8C%9B';
var statusVal = decodeURIComponent(statusValEncoded); // ⌛
按要求发送 Emoji 字符 body。例如:
that.http.post(Constants.API_URL + '/post', {status: '⌛'}, {headers: headers})
尽管表情符号是纯文本,但我无法通过 HTTP header 发送表情符号。
错误是表情符号不是有效的 HTTP header 字段值。
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);
}
请告诉我哪里做错了。
对于 HTTP header 值,只有 ASCII 字符可以保证有效。大多数 HTTP 客户端不允许使用其他字符,例如表情符号或中文字符。之前的一些讨论请参考What character encoding should I use for a HTTP header?。
在HTTP请求中发送Emoji,有2种方法:
编码表情符号字符并将其作为 header 发送。例如:
var status = encodeURIComponent('⌛'); // %E2%8C%9B headers.set('status', status);
当服务器收到请求后,对其进行解码:
var statusValEncoded = '%E2%8C%9B'; var statusVal = decodeURIComponent(statusValEncoded); // ⌛
按要求发送 Emoji 字符 body。例如:
that.http.post(Constants.API_URL + '/post', {status: '⌛'}, {headers: headers})