RESTful
Jakarta REST (JAX-RS) exceptions mapped to HTTP status codes, and testing REST endpoints with curl: JSON, form data and file uploads.
4 min readUpdated
A RESTful API maps operations on resources to HTTP methods and reports errors with HTTP status codes. This note covers both sides: the exception classes that Jakarta RESTful Web Services (JAX-RS, jakarta.ws.rs since version 3.0) provides for error responses, and the curl options and commands for calling an API with JSON, form data and file uploads.
Return HTTP errors with JAX-RS exceptions
JAX-RS 2.0 added an exception hierarchy for common HTTP error conditions. Instead of creating a WebApplicationException and passing it a status code, throw the exception that matches the error. The runtime turns it into a response with the right status:
| Exception | Status code | Meaning |
|---|---|---|
BadRequestException |
400 | Malformed message |
NotAuthorizedException |
401 | Authentication failure |
ForbiddenException |
403 | Access not permitted |
NotFoundException |
404 | Resource not found |
NotAllowedException |
405 | HTTP method not supported |
NotAcceptableException |
406 | Media type requested by the client not supported |
NotSupportedException |
415 | Media type posted by the client not supported |
InternalServerErrorException |
500 | General server error |
ServiceUnavailableException |
503 | Server temporarily unavailable or busy |
For example, in a resource method:
throw new NotFoundException("Order " + id + " not found");Common curl options
These curl options come up most often when testing an API:
-#,--progress-bar: show a simple progress bar instead of the standard progress meter.-b,--cookie <name=data>: send a cookie with the request. Without=, the argument is the name of a cookie file to read (see-c).-c,--cookie-jar <file>: save the cookies from the response to a file.-d,--data <data>: send data in a POST request (details below).-f,--fail: fail without printing the server’s error page when the response is an HTTP error.-F,--form <name=content>: submit form data asmultipart/form-data.-H,--header <header>: add a request header.-i,--include: include the response headers in the output.-I,--head: fetch the headers only.-k,--insecure: allow TLS connections to servers with invalid certificates.-L,--location: follow redirects.-o,--output <file>: write the output to a file. Add--create-dirsto create the directories in the path.-O,--remote-name: write the output to a file named like the remote file, in the current directory.-s,--silent: quiet mode. Add-Sto still show errors.-v,--verbose: show more information, useful for debugging.-w,--write-out <format>: print information after the transfer; see the manual for the available variables.-w "\n"appends a newline to the output, and you can put it in~/.curlrc.-X,--request <method>: the HTTP method to use.
Send data with POST and PUT
When you send data with a POST or PUT request, the Content-Type header tells the server its format. The two common formats are:
application/jsonapplication/x-www-form-urlencoded
Many APIs accept both. On the command line the form format is often easier, because JSON needs extra quoting, and curl -d sends form data by default, while JSON needs an explicit Content-Type header.
The options to combine:
- Request method:
-X POSTor-X PUT.-dalone already implies POST. - Content type header:
-H "Content-Type: application/x-www-form-urlencoded"-H "Content-Type: application/json"
- Data:
- form:
-d "param1=value1¶m2=value2"or-d @data.txt - JSON:
-d '{"key1":"value1", "key2":"value2"}'or-d @data.json
- form:
Examples
POST form data (application/x-www-form-urlencoded)
application/x-www-form-urlencoded is the default for -d:
curl -d "param1=value1¶m2=value2" -X POST http://localhost:3000/dataThe same with an explicit header:
curl -d "param1=value1¶m2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/dataRead the data from a file. @ tells curl that the argument is a file name:
curl -d "@data.txt" -X POST http://localhost:3000/dataPOST JSON (application/json)
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/dataFrom a file. The header is still needed, because curl does not derive the content type from the file name:
curl -d "@data.json" -H "Content-Type: application/json" -X POST http://localhost:3000/datacurl 7.82 and later have a --json option that sends the data and sets both Content-Type and Accept to application/json:
curl --json '{"key1":"value1", "key2":"value2"}' http://localhost:3000/dataUpload a file
-F sends a multipart/form-data request, like an HTML form with a file field. @ before the path uploads the file’s contents:
curl -X POST -F 'image=@/path/to/pictures/picture.jpg' http://domain.tld/uploadPOST multipart form fields
Add one -F for every field=value pair:
curl -X POST -F 'username=alice' -F 'password=something' http://domain.tld/post-to-me.phpTo send raw data of a specific type instead, set the header with -H and the body with -d:
curl -X POST -H 'Content-Type: application/json' -d '{"username":"alice","password":"something"}' http://domain.tld/login