1

I am triggering the AWS SNS through the API Gateway.

JSONObject requestBody = new JSONObject();
requestBody.put("phone_number", receiverNumber);
requestBody.put("sender_id", senderAlias);
requestBody.put("message_text", messageText);

This JSONObject is being sent to the api gateway as a ByteArrayInputStream throug the AWS SDK for Java v1. There are "\n" in the text, to create line breaks. The sms however does not have a new line there, it just prints \n.

In the Api Gateway the message is extracted like this: method.request.body.message_text

How do I have to set up the messageText variable to print new lines in the SMS? I tried replacing it with \n or \\n or \\\\n.. Also tried ASCII, didn't work.

Invocation

As this is a quite complex programm I can't show all of it. It's triggered via Insomnia with a String in Json format like this: Insomnia

It has to be a double backslahed n because thats just how the code needs it. The aws integration is an additional provider so it has to fit in already existing frames. The json object looks like this before being executed. Json Object

So I need to find a way to manipulate the string thats going in the object. But I don't know how.

EDIT 3: Deleting previous edits, as they were not helpful and did not target the problem as I know now.

Finally closing down the issue. It's a problem in the API-Gateway. The object reaches the gateway just fine, with a \n. Which would work in the SNS Service. But to trigger the SNS Service, it's all going into one URL, which converts the \n into %5Cn

Before transformation: enter image description here

URL: enter image description here

So the problem is in the URL encoding..

AnanasXpress
  • 111
  • 1
  • 12
  • 1
    Are you sure you have actual newlines in the text, not bacslashes followed by n? Can you show an example of how you fill in the `messageText` (code in question, please, not comment). – RealSkeptic Sep 08 '20 at 13:00
  • Sure, you are right. "new line characters \n" is misleading. It's just \n – AnanasXpress Sep 08 '20 at 13:05
  • Again, I ask: what are the actual characters there? How do you put the value in? Is the character `\u000a` or `\u005C` followed by `\u006E`? – RealSkeptic Sep 08 '20 at 13:10
  • Edited the answer for you. Hope that helps for clarification. – AnanasXpress Sep 08 '20 at 13:21
  • Is it too much to ask that you print the individual characters of the variable `messageText` in your program and show us what they are? Not the JSON object, not the message you send in Insomnia, the actual variable that you are using in that program in order to fill the message? What are the unicode values of the characters? – RealSkeptic Sep 08 '20 at 13:32
  • look [here](https://stackoverflow.com/q/7587409/592355) and [here](https://help.clicksend.com/article/l3lmuk9olw-how-do-i-add-a-line-break-in-my-sms-message) – xerx593 Sep 08 '20 at 13:33
  • ...so `\r\n` *could* work (sometimes), but `
    ` or `%0a` should be real alternatives..
    – xerx593 Sep 08 '20 at 13:38
  • @RealSkeptic Sorry if I didn't understand you correctly. I hope now I added what you wanted to see. – AnanasXpress Sep 09 '20 at 07:50
  • I tried replacing \n with System.lineSeparator(). Then I had a look in the InputStream that is passed to the Api Gateway. It has the chars 92 114 92 110 => \r\n . I think the problem might be here, that it's not one char, that says linebreak. Those four should just be "10" – AnanasXpress Sep 09 '20 at 08:57
  • OK, now it's clear. Your message actually contains a newline character. But your most recent comment makes it unclear again, because you said the characters come from Insomnia, not from Java code. And you are not showing any input stream in the code above. – RealSkeptic Sep 09 '20 at 09:26
  • Sorry for the confusion. I understand where the problem is now and I hope I can clear it up, as I don't know how to fix it. Ignore my System.lineSeparator() comment. I undid that as you pointed out correctly that there is a newline character in the string. The problem is, that it has to be converted to a ByteArrayInputStream to be sent to aws. But when I use toString().getBytes() on the JSONObject it converts the char 10, the newline character into two chars: 92 and 110 for \ and n. So this is where it gets messed up. – AnanasXpress Sep 09 '20 at 09:47
  • I guess this is something I need to have a deeper look on. the new line character has two bytes so I wont be able to fit it in one.. So I need another way to give it to aws. Gonna read the docu again. Thanks for the help so far @RealSkeptic ! – AnanasXpress Sep 09 '20 at 10:37
  • Does this answer your question? [AWS SNS how to add line breaks in message](https://stackoverflow.com/questions/44787953/aws-sns-how-to-add-line-breaks-in-message) – stdunbar Sep 11 '20 at 19:46
  • @stdunbar sadly, no. So far I am pretty sure, the problem is not with my Java Code. It's fine and the behaviour how it is split up into two bytes is also fine I think. The problem is the handling in the api gateway. Right now I am trying to create a templating model in the apigateway to decode the string correctly. – AnanasXpress Sep 14 '20 at 07:33

1 Answers1

1

Thanks to the AWS Support I now am able to send SMS with line breaks through the api gateway. It was wrong to use URL Query Parameters. I removed all of them

I needed one HTTP Header: Content-Type: 'application/x-www-form-urlencoded'

Then I used a Messaging Template like this, with passthrough: Never:

#set($message = $input.path('$.message_text'))
#set($phoneNumber = $input.path('$.phone_number'))
Action=Publish&PhoneNumber=$util.urlEncode($phoneNumber)&Message=$util.urlEncode($message)&MessageAttributes.entry.1.Name=AWS.SNS.SMS.SenderID&MessageAttributes.entry.1.Value.DataType=String&MessageAttributes.entry.1.Value.StringValue=Alias

Having my JSON Object in the Request like this:

{
"phone_number": "+4912345678",
"message_text": "Break\nHere",
"sender_id":"Alias"
}

Works perfectly fine with a line break in the SMS

AnanasXpress
  • 111
  • 1
  • 12