Encoding JSON and updating

152 views
Skip to first unread message

David Legard

unread,
Nov 19, 2017, 2:05:36 AM11/19/17
to Elm Discuss
I am using a simple JSON decode and encode (following from examples at elm-tutorial), 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
       
}

David Andrews

unread,
Nov 19, 2017, 4:48:43 AM11/19/17
to Elm Discuss
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.


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Legard

unread,
Nov 19, 2017, 5:35:53 AM11/19/17
to Elm Discuss
I'm testing it on localhost.

Roman Frołow

unread,
Nov 19, 2017, 9:37:10 AM11/19/17
to Elm Discuss
For uploading files I typically forward people to https://github.com/simonh1000/file-reader/tree/master/example

Steve Schafer

unread,
Nov 19, 2017, 1:27:23 PM11/19/17
to Elm Discuss
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.)

Steve Schafer

unread,
Nov 19, 2017, 1:31:03 PM11/19/17
to Elm Discuss
One other thing: You need to add a Content-Type: application/json header to the request.


On Sunday, November 19, 2017 at 5:35:53 AM UTC-5, David Legard wrote:

David Legard

unread,
Nov 20, 2017, 7:53:02 AM11/20/17
to Elm Discuss
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 = "{}" }


Steve Schafer

unread,
Nov 20, 2017, 9:03:24 AM11/20/17
to Elm Discuss
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.

David Legard

unread,
Nov 20, 2017, 7:22:39 PM11/20/17
to Elm Discuss
OK, thanks, I didn't fully understand your previous message.

I'll do some more tinkering with this.

Reply all
Reply to author
Forward
0 new messages