[Ur] JSON web service/api?

Artyom Shalkhakov artyom.shalkhakov at gmail.com
Wed Mar 8 23:59:46 EST 2017


Hello Isaac,

2017-03-09 0:51 GMT+06:00 Isaac Torbett <izacht13 at gmail.com>:
> I'm trying to get the urweb server to return json, but what is the type/kind
> I should put in the transaction monad?
>
>
> val main : <here> -> transaction page
>
>
> What would I put in <here>?
>

You should put [unit] (which is the same as {} aka the empty record).
The expression of this type would be () (empty parens), so you can
write:

> fun main () = ... (* some code *)

You could also write:

> fun main (_ : unit) =  ... (* some code *)

which means there is no pattern matching involved. There's only one
element in type unit, and it's (). So it doesn't make much sense to
pattern-match on it, but we still do it, presumably to let the type
inference work out its type for us. :-)

> Furthermore- I don't even know if my json-like structure is allowed, the
> compiler isn't complaining about it, so maybe it's fine.
>

To return JSON instead of HTML, you will have to write something along
the lines of:

> fun main () =
> let
>   val myJSON = "{\"foo\": 1}" (* the JSON, serialized as a string *)
> in
>   returnBlob (textBlob myJSON) (blessMime "application/json")
> end

And also, add this to your .urp file:

> allow mime application/json

To *produce* JSON (serialized to a string), you can use the JSON
library, which is part of the Ur/Web distribution.

I was planning on writing an article detailing how to create a simple
"modern" CRUD app in Ur/Web, that works very similarly to what you can
write with Angular, OpenUI5 or React. Stay tuned.

Using Ur/Web for creating a lightweight web service is also a
possibility. You can even run it on your smart toaster! :-)

>
> fun main () = return queryX1 (SELECT topic.Title, topic.Body FROM topic)
>
>
> I'm not even sure I know what I'm doing, `completely new to ML-style
> languages.
>
> - Isaac
>
> P.S. I've never actually used a Mailing List before, so if my post is
> malformed, please tell me.
>
> _______________________________________________
> Ur mailing list
> Ur at impredicative.com
> http://www.impredicative.com/cgi-bin/mailman/listinfo/ur
>



-- 
Cheers,
Artyom Shalkhakov



More information about the Ur mailing list