I need to expose a Spray service that accepts JSON payload. Where can I find a sample that would demonstrate such a feature?
To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON.
A RESTful web service usually defines a URI, Uniform Resource Identifier a service, provides resource representation such as JSON and set of HTTP Methods.
First, the contents of the file should be converted to String. Then we should read the file content and convert it to Byte data type. Once the entire data is converted to Byte, we should finally convert it to string. We shall utilize an external JSON file as a payload for executing a POST request.
When I started learning Spray I used this awesome sample project. It shows you how to structure your application and much more.
If you want to have a look at how it uses spray-json at the route level, then take a look at this example.
Hope it helps! (kudos to Jacobus for S4)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With