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

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
_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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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

We're in Beta

About us Mission Statement