Content type examples

The following examples summarize handling content in HTTP requests and responses. These examples are intended to show concepts and not exact HTTP protocols.

HTTP GET and DELETE requests specify all of their input parameters on the URI using standard encoding. There is no body in such requests. The content type of the requests for these is always x-www-form-urlencoded, since that is all that is allowed for GET and DELETE requests by the HTTP protocol. The client indicates what content type it can accept in the response by supplying an Accept header.

In the GET request below, the parameter is passed as a query string on the URI. The content type acceptable in the response is specified in the Accept header field as application/xml.

GET http://www.myHost.com:9080/myApp/rest/myCustomer?name=john%20doe
Accept: application/xml

All HTTP responses carry their data in their body, no matter what kind of request prompted the response. Responses begin with header fields. The Content-Type header field in a response tells the client what type of data is contained in the body below. HTTP allows many different content types in responses, but ZIETrans JAX-RS supports only application/xml and application/json. After the header fields and a blank line, the body begins, and contains the data in the specified format.

In the GET response below, the Content-Type header field specifies the type of data contained in the response body.

Content-Type: application/xml

<customer>
 <firstname>John</firstname>
 <lastname>Doe</lastname>
 <accountnumber>111111</accountnumber>
</customer>

By convention, a POST request typically asks to create something. The data required to fulfill the request is carried in the request body. The Content-Type indicates to the server the format of the request data. The Accept header again specifies the desired response format.

In the POST request below, the content type is specified in the Content-Type header field as application/xml and the content is supplied in the request body. The Accept header field specifies the acceptable format for data in the response, as application/json.

POST http://www.myHost.com:9080/myApp/rest/myCustomer
Content-Type: application/xml
Accept: application/json

<customer>
 <firstname>Jane</firstname>
 <lastname>Doe</lastname>
 <accountnumber>222222</accountnumber>
</customer>

In the POST response below, notice the format of the response does not have to match the format used in the request.

Content-Type: application/json

{"message":{
  "type": "resultCode",
  "value": "Jane Doe account created successfully"
}} 
The ZIETrans JAX-RS output format rule is to respond using the format specified in the Accept HTTP request header unless the client specifies a format using the URI query parameter alt as shown below:
POST http://www.myHost.com:9080/myApp/rest/myCustomer?alt=application/xml