Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can/should I use YAML as payload in RESTful webservice?

As the header says.

In general I like YAML more than JSON these days. I implemented a RESTful WS PoC back in the day using JSON. I was wondering if I can instead use YAML or not.

E.g. are there enough tools/libraries/support for doing that? Or would I end up doing quite a bit of mundane/tedious coding which I would've avoided if I were using JSON instead?

Also as I understood from WWW: REST doesn't restrict one from using YAML as the payload, is that correct?

Thanks!

like image 265
Kashyap Avatar asked Oct 27 '15 23:10

Kashyap


People also ask

What is payload in REST API?

What is Payload in REST API? In this short article, we will learn what is payload in REST API. What is Payload in REST API? In simple words, the payload means body in the HTTP request and response message. It's optional and depends on the HTTP method name i.e., -In the case of GET HTTP method, the HTTP request message without a body.

Is it possible to issue a request with a YAML payload?

As we can see when trying to issue a request with a YAML payload, there is no built-in support for YAML by default. At least for the Media Type I would have expected to be supported.

Should I use YAML or JSON for my API?

YAML being a superset of JSON, it may be interesting to also support it for your API consumers. In certain cases, YAML can provide more benefits, like much more readability to the API payload.

What is RESTful web service?

Restful Web Service, expose API from your application in a secure, uniform, stateless manner to the calling client. The calling client can perform predefined operations using the Restful service. The underlying protocol for REST is HTTP. REST stands for REpresentational State Transfer.


1 Answers

Yes, if it's a goal that the data be especially readable by humans. REST itself isn't focused on protocols/formats so much as patterns.

There's not a lot to gain here for webservices however, which typically represent app to app communication. Computers don't care, and JSON can be pretty-printed to improve legibility somewhat.

YAML is well supported by mainstream languages, though not always included in standard libraries as JSON typically is. So you'll probably be looking at an additional library dependency. Also, if the client is a browser, parsing will be slower, as you'll have to use a non-native external lib such as described here using: JavaScript YAML Parser . Make sure it gets compressed in transit or the extra indentation spaces will expand the size of the data.

Also, YAML has a lot of esoteric and downright potentially dangerous features. Whenever I'm using it I use the "safe" parser, and deactivate many if not most of its features besides data structures.

I could imagine some utility as a debug parameter however, perhaps url.yaml or …?fmt=yaml to assist during development. But, otherwise not much gain for all the trouble.

like image 57
Gringo Suave Avatar answered Sep 22 '22 14:09

Gringo Suave