Question
- How to add a new line in SMS using the Nexmo REST API?
- How can I include a carriage return in a message?
Answer
A newline character (\n or LF) should be represented in your HTTP request with 1 character in UTF8: %0A
CRLF (Carriage Return Line Feed) should be represented with 2 characters in UTF8: %0D%0A
Examples
The following code snippets demonstrate how to add newline characters to SMS messages:
PHP:
<?php $url = 'https://rest.nexmo.com/sms/json?' . http_build_query( [ 'api_key' => 'KEY', 'api_secret' => 'SECRET', 'to' => 'TO_NUMBER', 'from' => 'FROM_NUMBER', 'text' => "Here is a newline\nWow!" ] ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); echo $response;
Python:
import urllib import urllib2 params = { 'api_key': 'KEY', 'api_secret': 'SECRET', 'to': 'TO_NUMBER', 'from': 'FROM_NUMBER', 'text': 'Here is a newline:\nWow!' } url = 'https://rest.nexmo.com/sms/json?' + urllib.urlencode(params) request = urllib2.Request(url) request.add_header('Accept', 'application/json') response = urllib2.urlopen(request) print response.read()