jxxcarlson
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
$.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
[{"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!
{: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
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 21 to 12- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jxxcarlson
Will do, thanks!
benwilson512
Looks like my work here is done
@jxxcarlson I highly recommend hopping on the slack channel #absinthe-graphql, there’s always quite a few people around who can help as well.
jxxcarlson
It is indeed awesome … there is an Elm version as well, so I will use it for both Phoenix and Elm. Yay!
OvermindDL1
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!). ^.^
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.
Trust me to make all possible mistakes
Thanks so much for your help!
OvermindDL1
Oh @benwilson512, where are you?
OvermindDL1
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…
jxxcarlson
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 in GraphiQL, I get a good error message:
and I see activity in the server log. However, if I use the query
by analogy with 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.
“”"
OvermindDL1
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 havinggraphqlcommunicate over?jxxcarlson
I’m seeing this at lines 26-31 – were you in the graphQL branch?