0

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?

TheoK
  • 3,601
  • 5
  • 27
  • 37
  • `"my\Password" == "myPassword" #⇒ true`. Do you want to escape exactly what? The string containing a slash inside? Then use `"my\\Password"` and whoever would _decode_ it, will get `"my\\Password"` as desired. – Aleksei Matiushkin Apr 22 '20 at 07:16
  • Take a look at sigils https://stackoverflow.com/questions/33185095/how-do-you-embed-double-quotes-an-elixir-string – GavinBrelstaff Apr 22 '20 at 08:21
  • 1
    @GavinBrelstaff I doubt sigils would help here. `"my\\Password"` is pretty much like what they need. The encoded result just _seems_ obscure, but it’s correct. – Aleksei Matiushkin Apr 22 '20 at 08:31
  • Just to rephrase what @AlekseiMatiushkin is saying - there doesn't seem to be anything wrong with what's happening in your example. The final result is an Elixir representation of a string containing the JSON-encoded Elixir data structure. Perhaps if you gave more details as to when you're experiencing problems or what were you expecting to happen instead we could be more helpful. – Paweł Obrok Apr 22 '20 at 11:31

0 Answers0