Link Search Menu Expand Document

8. Post a Random Vehicle: Exercise

Extend the Autobarn API Client with two new commands:

  • r will create one random vehicle
  • l will loop, creating a random vehicle every 1 second until you press any key, at which point it returns to the main menu.

To create a vehicle, send an HTTP POST to https://autobarn.dev/api/vehicles/

The POST should have the Content-Type application/json and match the following example schema:

{
	"registration": "ABCD1234",
	"modelCode": "volkswagen-beetle",
	"year": 1982,
	"color": "yellow"
}
  • The vehicle modelCode should be selected at random from the codes retrieved from the API in the previous exercise.
  • The year should be a random integer between 1960 and 2025
  • The color should be one of the CSS named colours:
    • https://developer.mozilla.org/en-US/docs/Web/CSS/named-color
  • The registration should be 8 randomly generated characters in the range A-Z 0-9.

API Responses

201 Created
display a success message including the vehicle details
400 Bad Request
display the error message from the response body
409 Conflict
display the error message from the response body

Hints:

  • System.Random.Shared provides an instance of the System.Random random number generator you can use to generate random numbers; use the .Next(10) method to generate a random integer between 0 and 10