Discussion:
Encoding JSON and updating
(too old to reply)
David Legard
2017-11-19 07:05:35 UTC
Permalink
I am using a simple JSON decode and encode (following from examples at
elm-tutorial <https://www.elm-tutorial.org/en/>), but I'm clearly missing
something.

The *db.json* file has the simple structure

{
"plrs": {
"ixd": "FF",
"name": "Frank",
"level": 3
}
}


.. and I decode that successfully into an Elm object with an HTTP Get: from *http://localhost:3000/db.
*The Elm object holding the data has the type* Roster.*

When I encode it, and try to save it with an HTTP Send (to the identical
URL), I get a *File Not Found 404* error.

Do I need to tweak the URL for sending the data back, or would a mistake in
my Encoding code (quite a possibility) cause this to happen?

I'm running this as Administrator in case there are any permission issues.

I am using the code in the tutorial to create the HTTP request, as follows:

savePlayerRequest : Roster -> Http.Request Roster
savePlayerRequest roster =
Http.request
{ body = plrsEncoder roster |> Http.jsonBody
, expect = Http.expectJson plrsDecoder
, headers = []
, method = "PATCH"
, timeout = Nothing
, url = savePlayerUrl
, withCredentials = False
}
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Andrews
2017-11-19 09:48:35 UTC
Permalink
This depends on the backend server you are using and its configuration. It
has nothing to do with Elm.

I recommend against running your server as Administrator as a security
practice.


On Nov 19, 2017 2:05 AM, "David Legard" <***@gmail.com> wrote:

I am using a simple JSON decode and encode (following from examples at
elm-tutorial <https://www.elm-tutorial.org/en/>), but I'm clearly missing
something.

The *db.json* file has the simple structure

{
"plrs": {
"ixd": "FF",
"name": "Frank",
"level": 3
}
}


.. and I decode that successfully into an Elm object with an HTTP Get:
from *http://localhost:3000/db
<http://localhost:3000/db>. *The Elm object holding the data has the type*
Roster.*

When I encode it, and try to save it with an HTTP Send (to the identical
URL), I get a *File Not Found 404* error.

Do I need to tweak the URL for sending the data back, or would a mistake in
my Encoding code (quite a possibility) cause this to happen?

I'm running this as Administrator in case there are any permission issues.

I am using the code in the tutorial to create the HTTP request, as follows:

savePlayerRequest : Roster -> Http.Request Roster
savePlayerRequest roster =
Http.request
{ body = plrsEncoder roster |> Http.jsonBody
, expect = Http.expectJson plrsDecoder
, headers = []
, method = "PATCH"
, timeout = Nothing
, url = savePlayerUrl
, withCredentials = False
}
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Legard
2017-11-19 10:35:53 UTC
Permalink
I'm testing it on *localhost*.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Roman Frołow
2017-11-19 14:37:10 UTC
Permalink
For uploading files I typically forward people
to https://github.com/simonh1000/file-reader/tree/master/example
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Steve Schafer
2017-11-19 18:27:22 UTC
Permalink
The tutorial uses json-server, which, by default, expects PATCH requests to
go to a specific player's URL, not the top-level URL for all players. I
haven't played with the tutorial myself, so the following may not be 100%
accurate, but I think that you need to:

1. Give each player an ID field, and call that field "id".
2. When you perform the patch, the URL should be
http://localhost:3000/db/plrs/<id>. That is, the ID should be the final
part of the URL. (I'm not sure about the first part of the URL; your
example is not quite consistent with the json-server documentation.)
Post by David Legard
I'm testing it on *localhost*.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Steve Schafer
2017-11-19 18:31:02 UTC
Permalink
One other thing: You need to add a *Content-Type: application/json *header
to the request.
Post by David Legard
I'm testing it on *localhost*.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Legard
2017-11-20 12:53:02 UTC
Permalink
OK, thanks for the suggestions.

There's something going on beyond my comprehension level.

I can read the data from http://localhost:3000/db, but when I try to send
it back, even with an Http.emptyBody, I get:

BadStatus { status = { code = 404, message = "Not Found" }, headers = Dict.fromList
[("Cache-Control","no-cache"),("Content-Type","application/json;
charset=utf-8"),("Expires","-1"),("Pragma","no-cache")], url =
"http://localhost:3000/db", body = "{}" }
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Steve Schafer
2017-11-20 14:03:24 UTC
Permalink
Like I said in my previous message, you can't send data to that URL. That
URL identifies the *entire* database. With PUT, PATCH, and DELETE, you have
to use a URL that identifies a *specific *record in the database.

The error message also says that the body of the request is empty, but I
can't tell from the message itself what the underlying problem is with that.
Post by David Legard
OK, thanks for the suggestions.
There's something going on beyond my comprehension level.
I can read the data from http://localhost:3000/db, but when I try to send
BadStatus { status = { code = 404, message = "Not Found" }, headers = Dict
.fromList [("Cache-Control","no-cache"),("Content-Type","application/json;
charset=utf-8"),("Expires","-1"),("Pragma","no-cache")], url = "
http://localhost:3000/db", body = "{}" }
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Legard
2017-11-21 00:22:39 UTC
Permalink
OK, thanks, I didn't fully understand your previous message.

I'll do some more tinkering with this.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...