theoks

theoks

How to escape a single backslash in Elixir when encoding to JSON?

I have this body for a request, which in Elixir is represented like this:

body = %{
  password: "my\Password"
}

I use Poison.encode! to convert it to JSON and what it is being sent is this:

%HTTPoison.Request{
    body: "{\"sftp_password\":"\myPassword\"}
  }

If I try and escape it with a double backslash:

body = %{
  password: "my\\Password"
}

this is what is being encoded and sent in the request:

%HTTPoison.Request{
    body: "{\"sftp_password\":"\my\\\\Password\"}
  }

I also tried to convert the string to a charlist and it just encodes the code points, not the actual characters.

Is there any way to encode just one backslash, or to put it more generally: how do I pass string literals when encoding with Poison or in Elixir in general?If your question is related to Phoenix or Nerves, please post it in the Phoenix or Nerves Questions / Help section. Thanks!

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You don’t actually have one backslash in the original string. See:

iex(9)> body = %{password: "my\Password"}
%{password: "myPassword"}

Notice how the output is just "myPassword". The backslash is gone there, it doesn’t have anything to do with Poison’s JSON encoding. \ is itself the escape character in a string. If you want one in there literally, you need to escape it:

iex(1)> IO.puts("my\Password")
myPassword
:ok
iex(2)> IO.puts("my\\Password")
my\Password
:ok

Notably, if you’re reading in external text that has a slash, this will not be lost:

iex(3)> string = IO.gets("Password: ")
Password: my\Password
"my\\Password\n"
iex(4)> IO.puts string
my\Password
fuelen

fuelen

It is correctly encoded with double backslash and only one backslash is sent in request.
Multiple backslashes are used because both Elixir and JSON use them for escaping special characters.
This is just visual representation in terminal.

iex(1)> body = %{password: "my\\Password"}
%{password: "my\\Password"}
iex(2)> Poison.encode!(body)              
"{\"password\":\"my\\\\Password\"}"
iex(3)> Poison.encode!(body) |> IO.puts   
{"password":"my\\Password"}
:ok

Last result is a JSON that will be sent to the server.

LostKobrakai

LostKobrakai

body = %{password: ~S(my\Password)} and you don’t need to escape the backslash. Sigils can be wrapped by many different characters so unless you use all of them you should find one, which isn’t problematic: Syntax reference — Elixir v1.16.0

al2o3cr

al2o3cr

Try this in your browser’s Javascript console:

alert("my\\Password")

From RFC 8259:

   Alternatively, there are two-character sequence escape
   representations of some popular characters.  So, for example, a
   string containing only a single reverse solidus character may be
   represented more compactly as "\\".
Nicd

Nicd

I don’t understand this part. Looking at Swift’s docs, it has a similar string syntax to Elixir and also has backslash escapes, so you would need to write "foo\\bar" to get a string that contains foo\bar. Or am I mistaken?

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement