API JS cannot send Emojis in API body | Community
Skip to main content
Level 2
September 8, 2023
Solved

API JS cannot send Emojis in API body

  • September 8, 2023
  • 2 replies
  • 1465 views

Hi everyone,

I'm using an api to send messages but the problem is that when I insert an emoji in the body I get a 400 Bad request back here is my js code :

var jsonData = { "type": "text", "text": "body": "hello how are you ", token = "xxxxxxxxxxx", contentType = "application/json"; var req = new HttpClientRequest('https://exampleTestEndpoint.com/sendMessage/128192'); req.header["Authorization"] = token; req.header["Content-Type"] = contentType; req.method = "POST"; var data = jsonData; req.body = JSON.stringify(data); req.execute(); var resp = req.response; var code = resp.code; var body = JSON.parse(resp.body);

Then when I remove the emoji everything works normally, I tried with another api from another service and I had the same problem when I insert an emoji,
I also tried with the utf-8 code of the emoji but it still doesn't work.

 

Thank you in advance for your help!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by AnasSpir05

Hello everyone,
I've found the solution to the problem, which was to change the encoding in the header and replace this value in the header

var contentType = "application/json;charset=UTF-8"; req.header["Content-Type"] = contentType;

most modules take it into account automatically, but on adobe campaign you have to specify it.

2 replies

Adobe Employee
September 8, 2023

HI @anasspir05 

I would suppose you need to send the emojis in their unicode value. Emojis are unique code values and JSON is using unicode too, 

This page is old https://stackoverflow.com/questions/7177808/emoji-characters-cannot-be-encoded-to-json#:~:text=If%20you%20want%20to%20send,request%20when%20sending%20the%20message. and it seems that there are a few tricks to put emojis in JSON

Now how to get unicode for a emojis, I don't now exactly and you certainly can find the how by search internet (Stackoverflow or other)

Thanks

Denis

AnasSpir05AuthorAccepted solution
Level 2
September 8, 2023

Hello everyone,
I've found the solution to the problem, which was to change the encoding in the header and replace this value in the header

var contentType = "application/json;charset=UTF-8"; req.header["Content-Type"] = contentType;

most modules take it into account automatically, but on adobe campaign you have to specify it.