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"    }

First 5 of 5 Posts Switch mode

NobbZ

NobbZ

First difference I see is the UA.

So try setting a UA that is close to what curl does.

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.

NobbZ

NobbZ

PS: To make one thing clear, I do not see an “extra” slash (/) anywhere, only some backslash (\) which often is confusing.

Please make sure to understand that escaping it is a purely representational thing.

iex(1)> "{\"query\":\"https://lol.com\"}" == ~s<{"query":"https://lol.com"}>
true
axelson

axelson

Scenic Core Team

Can you show the full curl command that works?

lukeclopez

lukeclopez OP

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!

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement