The OpenStack Compute API supports both JSON and XML data serialization request and response formats.
You specify the request format in the
Content-Type
header in the request.
This header is required for operations that have a
request body. The syntax for the
Content-Type
header is:
Content-Type: application/format
Where format
is either
json
or xml
,
you specify the response format by using one of the
following methods:
Accept
header. The syntax for theAccept
header is:Accept: application/format
Where
format
is eitherjson
orxml
, the default format isjson
.Query extension. Add an
.xml
or.json
extension to the request URI. For example, the.xml
extension in the following URI request specifies that the response body is returned in XML format:POST /v2/010101/servers.xml
If you do not specify a response format, JSON is the default.
If you specify conflicting formats in the
Accept
header and the query
extension, the format specified in the query extension
takes precedence. For example, if the query extension
is .xml
and the Accept
header specifies application/json
,
the response is returned in XML format.
You can serialize a response in a different format from the request format. Example 1.1, “JSON request with headers” and Example 1.2, “XML response with headers” show a request body in JSON format and a response body in XML format.
Example 1.1. JSON request with headers
POST /v2/010101/servers HTTP/1.1
Host: servers.api.openstack.org
Content-Type: application/json
Accept: application/xml
X-Auth-Token: eaaafd18-0fed-4b3a-81b4-663c99ec1cbb
{ "server":{ "flavorRef":"http://openstack.example.com/openstack/flavors/1", "imageRef":"http://openstack.example.com/openstack/images/70a599e0-31e7-49b7-b260-868f441e862b", "metadata":{ "My Server Name":"Apache1" }, "name":"new-server-test", "personality":[ { "contents":"ICAgICAgDQoiQSBjbG91ZCBkb2VzIG5vdCBrbm93IHdoeSBpdCBtb3ZlcyBpbiBqdXN0IHN1Y2ggYSBkaXJlY3Rpb24gYW5kIGF0IHN1Y2ggYSBzcGVlZC4uLkl0IGZlZWxzIGFuIGltcHVsc2lvbi4uLnRoaXMgaXMgdGhlIHBsYWNlIHRvIGdvIG5vdy4gQnV0IHRoZSBza3kga25vd3MgdGhlIHJlYXNvbnMgYW5kIHRoZSBwYXR0ZXJucyBiZWhpbmQgYWxsIGNsb3VkcywgYW5kIHlvdSB3aWxsIGtub3csIHRvbywgd2hlbiB5b3UgbGlmdCB5b3Vyc2VsZiBoaWdoIGVub3VnaCB0byBzZWUgYmV5b25kIGhvcml6b25zLiINCg0KLVJpY2hhcmQgQmFjaA==", "path":"/etc/banner.txt" } ], "networks":[ { "uuid":"c6a48469-4638-4346-b755-6e66ed7abdff", "fixed_ip":"10.0.0.10" } ] } }
Example 1.2, “XML response with headers” shows the headers and XML response returned by the JSON request:
Example 1.2. XML response with headers
HTTP/1.1 202 Accepted
Date: Mon, 23 Jul 2012 20:24:48 GMT
Content-Length: 582
Location: https://servers.api.openstack.org/v2/010101/servers/06dba123-2c7e-4639-bea0-09fbe219b056
Content-Type: application/xml
X-Compute-Request-Id: req-ab05045a-452f-4b46-be0d-86494da02a2b
Server: Jetty(8.0.y.z-SNAPSHOT)
<?xml version='1.0' encoding='UTF-8'?> <server xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://docs.openstack.org/compute/api/v1.1" id="ea8417a1-7957-4ed5-8b3c-0befc1724308" adminPass="FoFw84XtQj3c"> <metadata/> <atom:link href="http://openstack.example.com/v2/openstack/servers/ea8417a1-7957-4ed5-8b3c-0befc1724308" rel="self"/> <atom:link href="http://openstack.example.com/openstack/servers/ea8417a1-7957-4ed5-8b3c-0befc1724308" rel="bookmark"/> </server>
The following example shows an alternative method of
achieving the same result. The following request uses
an URI extension of .xml
instead of
an Accept
header to request an XML
response.
Note | |
---|---|
The XML response is not shown. |
Example 1.3. JSON request with XML query extension for the response
POST /v2/010101/servers.xml HTTP/1.1
Host: servers.api.openstack.org
Content-Type: application/json
X-Auth-Token: eaaafd18-0fed-4b3a-81b4-663c99ec1cbb
{ "server":{ "flavorRef":"http://openstack.example.com/openstack/flavors/1", "imageRef":"http://openstack.example.com/openstack/images/70a599e0-31e7-49b7-b260-868f441e862b", "metadata":{ "My Server Name":"Apache1" }, "name":"new-server-test", "personality":[ { "contents":"ICAgICAgDQoiQSBjbG91ZCBkb2VzIG5vdCBrbm93IHdoeSBpdCBtb3ZlcyBpbiBqdXN0IHN1Y2ggYSBkaXJlY3Rpb24gYW5kIGF0IHN1Y2ggYSBzcGVlZC4uLkl0IGZlZWxzIGFuIGltcHVsc2lvbi4uLnRoaXMgaXMgdGhlIHBsYWNlIHRvIGdvIG5vdy4gQnV0IHRoZSBza3kga25vd3MgdGhlIHJlYXNvbnMgYW5kIHRoZSBwYXR0ZXJucyBiZWhpbmQgYWxsIGNsb3VkcywgYW5kIHlvdSB3aWxsIGtub3csIHRvbywgd2hlbiB5b3UgbGlmdCB5b3Vyc2VsZiBoaWdoIGVub3VnaCB0byBzZWUgYmV5b25kIGhvcml6b25zLiINCg0KLVJpY2hhcmQgQmFjaA==", "path":"/etc/banner.txt" } ], "networks":[ { "uuid":"c6a48469-4638-4346-b755-6e66ed7abdff", "fixed_ip":"10.0.0.10" } ] } }