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

455 Views Asked by At

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?

0

There are 0 best solutions below