jaisanas

jaisanas

Jason.DecodeError on Phoenix Websocket with Cowboy2

Hi guys i have implemented websocket to my phoenix app. Before it success to connected and joined room to my websocket, but right now i got this error

[error] Ranch listener MarketPlaceDisbursementServiceWeb.Endpoint.HTTP had connection process 
started with :cowboy_clear:start_link/4 at #PID<0.499.0> exit with reason: {%Jason.DecodeError
{data: "{   \"topic\": \"room:marketplace_disbursement_service\",   \"event\": \"inquiry_response\", 
  \"payload\": {},   \"ref\": 0 }", position: 2, token: nil}, [{Jason, :decode!, 2, [file: 'lib/jason.ex', line: 78]}, {Phoenix.Socket.V1.JSONSerializer, :decode!, 2, [file: 'lib/phoenix/socket/serializers/v1_json_serializer.ex',
 line: 29]}, {Phoenix.Socket, :__in__, 2, [file: 'lib/phoenix/socket.ex', line: 498]}, '{Phoenix.Endpoint.Cowboy2Handler, :websocket_handle, 2, [file: 'lib/phoenix/endpoint/cowboy2_handler.ex', line: 77]}, {:cowboy_websocket, :handler_call, 6, [file: 
'/Users/jaisanas/work/backend/deps/cowboy/src/cowboy_websocket.erl', line: 471]}, {:cowboy_http, :loop,
 2, [file: '/Users/jaisanas/work/backend/deps/cowboy/src/cowboy_http.erl', line: 233]}, {:proc_lib, 
:init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}

and here is my mix.exs file:

      {:phoenix, "~> 1.4.0"},
      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 4.0"},
      {:ecto_sql, "~> 3.0"},
      {:postgrex, ">= 0.0.0"},
      {:gettext, "~> 0.11"},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.0"},
      {:httpoison, "~> 1.4"},
      {:poison, "~> 3.1"}

On this thread github said that i have to delete rm -rf _build. But it still return error, anyone have a clue how to solve this error ?

Most Liked

NobbZ

NobbZ

<<194, 160>> is U+00A0, a no breaking space.

According to RFC 7159 it is not valid whitespace:

      ws = *(
             %x20 /              ; Space
             %x09 /              ; Horizontal tab
             %x0A /              ; Line feed or New line
             %x0D )              ; Carriage return
jaisanas

jaisanas

Hi guys, thanks for yor help, finally i have found the solution. The problem is my jason contains tab character. So what i have to do is just clear any additional character like newline or tab from:

"{   \"topic\": \"room:marketplace_disbursement_service\",   \"event\": \"inquiry_response\",   \"payload\": {},   \"ref\": 0 }"

to this one:

"{\"topic\":\"room:marketplace_disbursement_service\",\"event\":\"inquiry_response\",\"ref\":0}"

and it works , thanks guys for helping me that the problem is jason

kip

kip

ex_cldr Core Team

That does look like a bug if Jason doesn’t accept 0x09 as whitespace character. The JSON standard certainly suggests it should be valid. Perhaps submit a bug report?

It seems discourse replaces the 0x09 with space characters which is why my copy/paste of your snippet parsed ok.

OvermindDL1

OvermindDL1

Copy/pasted what you put and it works fine:

iex()50> a = "    {    \"a\": \"first\"    , \"b\": \"last\",        \"c\": {}        }"
"    {    \"a\": \"first\"    , \"b\": \"last\",        \"c\": {}        }"
iex()51> Jason.decode(a)
{:ok, %{"a" => "first", "b" => "last", "c" => %{}}}

Throwing in some random tabs is still fine too:

iex()52> a = " \t   {  \t  \"a\": \t\"first\"  \t  ,\t \"b\": \"last\",     \t   \"c\": {} \t
 }"
" \t   {  \t  \"a\": \t\"first\"  \t  ,\t \"b\": \"last\",     \t   \"c\": {} \t       }"
iex()53> Jason.decode(a)
{:ok, %{"a" => "first", "b" => "last", "c" => %{}}}

Are you ‘sure’ your IDE/sublime is not adding in some weird invalid unicode stuff or so? What if you take your not-working json string and assign it to a then run this:

inspect(a, binaries: :as_binaries)

What is the result of that please? :slight_smile:

jaisanas

jaisanas

it returns like below:

"<<123, 32, 194, 160, 194, 160, 34, 116, 111, 112, 105, 99, 34, 58, 32, 34, 114, 111, 111, 109, 58, 109, 97, 114, 107, 101, 116, 112, 108, 97, 99, 101, 95, 100, 105, 115, 98, 117, 114, 115, 101, 109, 101, 110, 116, 95, 115, 101, 114, 118, ...>>"

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement