Compiling, running, and testing

Compile this example and run it with the cargo run command. Use curl to send requests to the service. In the first request, we'll generate a random number with a uniform distribution:

$ curl --header "Content-Type: application/json" --request POST 
--data '{"distribution": "uniform", "parameters": {"start": -100, "end": 100}}'
http://localhost:8080/random

We sent a POST request to the localhost:8080/random URL with a JSON body. This will return {"value":-55.0}.

The next command requests a shuffle of the "1234567890" binary string converted to Base64:

$ curl --header "Content-Type: application/json" --request POST 
--data '{"distribution": "shuffle", "parameters": { "data": "MTIzNDU2Nzg5MA==" } }'
http://localhost:8080/random

The expected response will be {"bytes":"MDk3NjgxNDMyNQ=="}, which equals the string "0976814325". You'll get another value for this request.

The next request will take a random color:

$ curl --header "Content-Type: application/json" --request POST 
--data '{"distribution": "color", "parameters": { "from": "black", "to": "#EC670F" } }'
http://localhost:8080/random

Here, we used both representations of a color value: a string value of "black" and a hex value of "#EC670F". The response will be something similar to {"color":"#194A09"}.

The last example shows what happens if we try to send a request with an unsupported value:

$ curl --header "Content-Type: application/json" --request POST 
--data '{"distribution": "gamma", "parameters": { "shape": 2.0, "scale": 5.0 } }'
http://localhost:8080/random

Since the service doesn't support "gamma" distribution, it will return an error reading "unknown variant `gamma`, expected one of `uniform`, `normal`, `bernoulli`, `shuffle`, `color` at line 1 column 24".

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.131.255