Adding a New Line in SMS Using Vonage REST API
Objective
Adding a new line or carriage return in SMS using the Vonage REST API.
Applies To
- REST API
- SMS
Procedure
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
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()
Comments
3 comments
When we add %0A to the msg and Encode it to pass it to the Rest API it does not work, Rather just shows "%0A" in the content of the msg. What to do ?
Hello,
The complete URL should look like:
"api_key=KEY&api_secret=SECRET&from=Nexmo&to=44123456789&text=Thank+you.%0Athis+is+a+new+line"
I am guessing when you are encoding the string, it is double encoding the already encoded "%0A", so "%0A" becomes "%250A", its encoding the "%" which becomes "%25" giving you the "%250A" rather then the "%0A".
Hope this helps.
Kyle Hudson
I am trying to send text "TEST %0A line break + lets see how it shows."
If i do URLENCODE it shows + but no line break.
If i do HTMLENCODE it shows line break but no +
if i implement it it htmlencode first and url encode again no line break.
Can you help ?
Please sign in to leave a comment.