Skip to content

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 as multipart/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-dirs to 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 -S to 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/json
  • application/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 POST or -X PUT. -d alone already implies POST.
  • Content type header:
    • -H "Content-Type: application/x-www-form-urlencoded"
    • -H "Content-Type: application/json"
  • Data:
    • form: -d "param1=value1&param2=value2" or -d @data.txt
    • JSON: -d '{"key1":"value1", "key2":"value2"}' or -d @data.json

Examples

POST form data (application/x-www-form-urlencoded)

application/x-www-form-urlencoded is the default for -d:

Terminal window
curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/data

The same with an explicit header:

Terminal window
curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data

Read the data from a file. @ tells curl that the argument is a file name:

Terminal window
curl -d "@data.txt" -X POST http://localhost:3000/data

POST JSON (application/json)

Terminal window
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data

From a file. The header is still needed, because curl does not derive the content type from the file name:

Terminal window
curl -d "@data.json" -H "Content-Type: application/json" -X POST http://localhost:3000/data

curl 7.82 and later have a --json option that sends the data and sets both Content-Type and Accept to application/json:

Terminal window
curl --json '{"key1":"value1", "key2":"value2"}' http://localhost:3000/data

Upload 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:

Terminal window
curl -X POST -F 'image=@/path/to/pictures/picture.jpg' http://domain.tld/upload

POST multipart form fields

Add one -F for every field=value pair:

Terminal window
curl -X POST -F 'username=alice' -F 'password=something' http://domain.tld/post-to-me.php

To send raw data of a specific type instead, set the header with -H and the body with -d:

Terminal window
curl -X POST -H 'Content-Type: application/json' -d '{"username":"alice","password":"something"}' http://domain.tld/login

References

Related notes

2 min read

gRPC

What gRPC is, how it differs from JSON over REST, and its four kinds of RPC with a Protocol Buffers service example.

  • APIs & Integration
  • Architecture & Process
3 min read

Strava

Upload GPS tracks to Strava from the command line with curl: create an API application, get an OAuth token and call the uploads endpoint.

  • APIs & Integration
2 min read

Read Google Sheets as JSON

Read a public Google Sheet as JSON over HTTP, with the keyless gviz endpoint or the Sheets API v4 and an API key.

  • APIs & Integration

Type to search the whole site.