# Inconsistent API requests

**URL:** <https://forum.elixirforum.com/t/inconsistent-api-requests/5025>\
**Category:** Questions\
**Tags:** phoenix, troubleshooting\
**Created:** [May 10, 2017, 3:42pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025 "2017-05-10T15:42:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 3:42pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/1 "2017-05-10T15:42:43Z")

</div>

Hello! I have been using AJAX in the front end of my phoenix app to give flickerless updates when text is edited and rendered (see AJAX code below). The fields of `data` in the Ajax code are available in the reply to Phoenix in `params`, e.g., `params["title"]`.

I would like to use the api server more generally and have been testing it with Postman. I do this by creating a key-value pair for the `headers` section, e.g., key=‘data’ value = {“title”: “TEST”, content: 'YADA YADA", …}. The info in the api server reply is in `conn.req_headers`, which is a list of key-value pairs as listed further down.

I can handle either case, but would like to formulate my requests and replies in a uniform manner – What do you advise?

**AJAX CODE**

```elixir
$.ajax({
        url: "/api/notes/" + note_id,
        type: "put",
        data: {
            title: title,
            username: username,
            user_id: user_id,
            token : token,
            content: content,
            tag_string: tag_string,
            identifier: identifier,
        },
        headers: {
            "X-CSRF-TOKEN": csrf
        },
        dataType: "json",
        success: function (data) {

          console.log("OKKKK!");
          console.log(data);
          document.getElementById("rendered_text3a").innerHTML = data.rendered_text;

          newTypeset();

        }
    });

```

**API REPLY TO POSTMAN REQUEST**

```elixir
[{"cache-control", "no-cache"},
 {"postman-token", "cee52325-1811-43b0-bd9c-413068e6b9d6"}, {"user_id", "9"},
 {"data",
  "{\"username\":\"jxxcarlson\",\"content\":\"This *is* a test\",\"tag_string\":\"foo,bar\",\"title\":\"Magick\"}"},
 {"token",
  "\"eyYADA_YADA_YADA_pPcVyg\""},
 {"username", "jxxcarlson"}, {"content", "This *is* a test"},
 {"title", "Magick"}, {"id", "1114"},
 {"user-agent", "PostmanRuntime/3.0.11-hotfix.2"}, {"accept", "*/*"},
 {"host", "localhost:4001"}, {"accept-encoding", "gzip, deflate"},
 {"content-length", "0"}, {"connection", "keep-alive"}]

```

**NOTE added** I extract info from `conn.req_headers` using the code below. Surely there is a better way!

```elixir
   {:ok, data} = Poison.Parser.parse conn2value(conn, "data")

    defp key2value(list, key) do
      pair = Enum.filter(list, fn(pair) -> {k, v} = pair; k == key end)
      [{_,value}] = pair
      value
    end

    defp conn2value(conn, key) do
      key2value(conn.req_headers, key)
    end

```

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [May 10, 2017, 5:13pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/2 "2017-05-10T17:13:02Z")

</div>

Well the Drab library for Elixir would let Elixir control the page for the same flickerless updates.

Absinthe for Elixir would let you use GraphQL instead of AJAX, which would be _MUCH_ more uniform to use and faster to process as well.

Or for still doing it as you are I would transfer the data in a post body and read it back from the body, I’d not mess with headers.

But still, parsing json is still json, I’d go with GraphQL instead of json straight.

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 5:14pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/3 "2017-05-10T17:14:10Z")

</div>

Thanks! I will start off by investigating GraphQL

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [May 10, 2017, 5:15pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/4 "2017-05-10T17:15:37Z")

</div>

> [@jxxcarlson](#):
>
> Thanks! I will start off by investigating GraphQL

A quick short, GraphQL is a json-like query language developed by facebook, you send the query to the server and the server fulfills it and send back json in the exact format the client wants it in based on the query.

Absinthe is a wonderous Elixir library that manages all the horror of it for you, fully typed and correct and handled and you just respond with the specific data that Absinthe asks you and it munges it in to how the client wants. 🙂

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 5:17pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/5 "2017-05-10T17:17:39Z")

</div>

re GraphQL – excellent! I hate my AJAX code. It is totally Rube Goldberg. I’ll check out Absinthe as well.

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [May 10, 2017, 5:27pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/6 "2017-05-10T17:27:02Z")

</div>

> [@jxxcarlson](#):
>
> re GraphQL – excellent! I hate my AJAX code. It is totally Rube Goldberg. I’ll check out Absinthe as well.

There is even an absinthe over phoenix sockets library out around somewhere too for even faster speed. There are a ton of javascript libraries to work with GraphQL too that make it almost stupid-simple to use. The Apollo front-end framework is practically built for GraphQL too (and there is a phoenix websocket thing for it as well).

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 5:36pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/7 "2017-05-10T17:36:08Z")

</div>

Stupid simple is what I need:-)

I’ll take a look at these others as well – I haven’t used sockets yet, but speed is important in this instance – real time editing / rendering of math/science text.

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 8:37pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/8 "2017-05-10T20:37:40Z")

</div>

I’ve run into some trouble getting `absinthe` to work on my app. The error message in the logs is

```elixir
[debug] ** (Phoenix.Router.NoRouteError) no route found for POST /graphql (LookupPhoenix.Router)
    (lookup_phoenix) web/router.ex:1: LookupPhoenix.Router.match_route/4
    (lookup_phoenix) web/router.ex:1: LookupPhoenix.Router.do_call/2
    (lookup_phoenix) lib/lookup_phoenix/endpoint.ex:1: LookupPhoenix.Endpoint.phoenix_pipeline/1
    (lookup_phoenix) lib/plug/debugger.ex:123: LookupPhoenix.Endpoint."call (overridable 3)"/2
    (lookup_phoenix) lib/lookup_phoenix/endpoint.ex:1: LookupPhoenix.Endpoint.call/2
    (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
    (cowboy) /Users/carlson/dev/elixir/ns_umbrella/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

```

The request in `graphiQL` is POST `http://localhost:4001/graphql` and the error message there is

```elixir
SyntaxError: Unexpected token < in JSON at position 0

```

In `web/router.ex` I have the code

```elixir
  forward "/graphiql", Absinthe.Plug.GraphiQL,
      schema: NoteApi.Schema

```

and in `phoenix.router.ex` I have added the code

```elixir
  defmodule NoteApi.Router do
      use NoteApi.Web, :router
      # ...
      forward "/graphql", Absinthe.Plug,
        schema: NoteApi.Schema
      # ...
    end

```

What would you suggest?

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [May 10, 2017, 8:42pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/9 "2017-05-10T20:42:00Z")

</div>

Do you have this on github somewhere? The reason is that `no route found for POST /graphql` really implies that your `forward "/graphql", Absinthe.Plug....` line is not in your main router (this is not an absinthe issue, purely routing setup, normal Phoenix stuff).

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 8:44pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/10 "2017-05-10T20:44:02Z")

</div>

Let me fiddle with it a bit – if I fail, I will push my graphQL branch to GitHub

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 8:52pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/11 "2017-05-10T20:52:35Z")

</div>

Well, my experiment didn’t work – here is the github link:

[https://github.com/jxxcarlson/ns\_umbrella](https://github.com/jxxcarlson/ns_umbrella)

The code is in the graphQL branch — look in apps/lookupPhoenix

the `forward code` is at

[https://github.com/jxxcarlson/ns\_umbrella/blob/graphQL/apps/lookup\_phoenix/web/router.ex#L30](https://github.com/jxxcarlson/ns_umbrella/blob/graphQL/apps/lookup_phoenix/web/router.ex#L30)

Thanks!

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [May 10, 2017, 8:56pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/12 "2017-05-10T20:56:08Z")

</div>

Hmm, I’m not seeing a `forward "/graphql", Absinthe.Plug....` line in your router file at:  
[https://github.com/jxxcarlson/ns\_umbrella/blob/graphQL/apps/lookup\_phoenix/web/router.ex](https://github.com/jxxcarlson/ns_umbrella/blob/graphQL/apps/lookup_phoenix/web/router.ex)

Without that there the route is not exposed at `/graphql`, just like any other controller. 🙂

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 8:59pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/13 "2017-05-10T20:59:28Z")

</div>

I’m seeing this at lines 26-31 – were you in the graphQL branch?

```elixir
  pipeline :api do
    plug :accepts, ["json"]
  end

  forward "/graphiql", Absinthe.Plug.GraphiQL,
      schema: NoteApi.Schema

```

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [May 10, 2017, 9:02pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/14 "2017-05-10T21:02:02Z")

</div>

> [@jxxcarlson](#):
>
> I’m seeing this at lines 26-31 – were you in the graphQL branch?

I’m seeing that too, but that is the forward for Absinthe.Plug.GraphiQL, which is the introspection helper path, but I’m not seeing a forward for `forward "/graphql", Absinthe.Plug, schema: NoteApi.Schema`, which is the actual path that you are having `graphql` communicate over?

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 9:16pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/15 "2017-05-10T21:16:23Z")

</div>

Some progress – I now understand your point about the “other” forward. I inserted it, and if I press PLAY in GraphiQL with an empty query and [http://localhost:4001/graphql](http://localhost:4001/graphql) in GraphiQL, I get a good error message:

```elixir
{
  "errors": [
    {
      "message": "No operations provided."
    }
  ]
}

```

and I see activity in the server log. However, if I use the query

```elixir
{
  notes {
    id
  }
}

```

by analogy with [https://www.dailydrip.com/topics/elixirsips/drips/graphql-with-absinthe](https://www.dailydrip.com/topics/elixirsips/drips/graphql-with-absinthe), then I get the error message  
“”"  
Variables are invalid JSON: Unexpected token n in JSON at position 4.  
“”"

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [May 10, 2017, 9:32pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/16 "2017-05-10T21:32:13Z")

</div>

> [@jxxcarlson](#):
>
> Variables are invalid JSON: Unexpected token n in JSON at position 4.

Uhh, you see that in the output? If you check the browser console what was the actual body that was received? I don’t really use GraphiQL much so I’m wondering if there is a bug with that…

Now where are the absinthe devs, I know there are here somewhere…

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [May 10, 2017, 9:33pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/17 "2017-05-10T21:33:14Z")

</div>

Oh @benwilson512, where are you? 🙂

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 9:36pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/18 "2017-05-10T21:36:36Z")

</div>

Oops, operator error once again – I had pasted the query into the lower “Query Variables” pane. When I put it where it should be – above – all is good.

Trust me to make all possible mistakes 🙂

Thanks so much for your help!

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [May 10, 2017, 9:37pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/19 "2017-05-10T21:37:49Z")

</div>

> [@jxxcarlson](#):
>
> Oops, operator error once again – I had pasted the query into the lower “Query Variables” pane. When I put it where it should be – above – all is good.

Ahh whoo, I thought I was going crazy there for a second, I could not figure out what was wrong. ^.^;

No problem! I quite like GraphQL, and Absinthe makes it awesome in Elixir. I’ve even called in to it from the back-end of the app at times too (yay report generation!). ^.^

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jxxcarlson/32/3634_2.png) [@jxxcarlson](https://forum.elixirforum.com/u/jxxcarlson)\
**Post date:** [May 10, 2017, 9:39pm UTC](https://forum.elixirforum.com/t/inconsistent-api-requests/5025/20 "2017-05-10T21:39:15Z")

</div>

It is indeed awesome … there is an Elm version as well, so I will use it for both Phoenix and Elm. Yay!

[Next page](https://forum.elixirforum.com/t/inconsistent-api-requests/5025.md?page=2)
