lukeclopez

lukeclopez

Elixir adds an extra slash in POST request body, making a URL inside it invalid

The approximate code:

    params = %{query: "https://www.lol.com"}
    params = Jason.encode!(params)
    HTTPoison.post(url, params, headers, @options)

The approximate request and response:

[
  {
    "request": {
      "body": "{\"query\":\"https://lol.com\"}",
      "url": "https://url.com",
      "headers": {
        "Authorization": "***",
        "Content-Type": "application/json"
      },
      "method": "post",
      "request_body": ""
    },
    "response": {
      "binary": false,
      "type": "ok",
      "body": "{\"message\":\"Bad Request\"}\n",
      "headers": {
        "content-type": "application/json",
        "content-length": "26"
      },
      "status_code": 400
    }
  }
]

Inspecting the body also returns "{\"query\":\"https://lol.com\"}"

As far as I can tell, it’s from the extra slash that comes after .com to escape the double quote.

What I’ve tried:
Building the body in a sigil like these:
~S({"query":"https://lol.com"})
~s<{"query":"https://lol.com"}>

Replacing the slash with String.replace

Using req instead of HTTPoison

This is a 3rd party API so I don’t have control over how their server receives it. Any further ideas for a client-side solution (the client being my Elixir code)?

I can get a curl request working with all the same params, but I can’t make it work in Elixir.

These seem to fail because Elixir always represents JSON object strings with escaped quotes.

curl 'https://url.com' 'Content-Type: application/json' -d '{\n "query": "https://lol.com"    }

Marked As Solved

lukeclopez

lukeclopez

Thank you everyone for your patience and your responses.

The issue was not the backslash (which I incorrectly called a slash) as you pointed out. The issue was that this request required Basic Auth, which evidently must be Base64 encoded in order to work. I was sending the auth string in plaintext.

Problem solved. Thank you!

Also Liked

Hermanverschooten

Hermanverschooten

This is odd, because those are just representational.

If you would do File.write("test.txt", Jason.encode(%{query: "https://www.lol.com"}), you would see no backslashes.

If I take your example of above:

"{\"query\":\"https://lol.com\"}" |> String.length()
"{'query':'https://lol.com'}" |> String.length()
Both of these will return 27.

So I don’t think this is your issue.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

Other popular topics Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement