카테고리 없음
2014. 1. 12. 11:30
반응형
근래 Web API를 개발 및 테스트를 하면 매우 유용하다.
https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Virtualization/3.0/html/REST_API_Guide/appe-REST_API_Guide-cURL_Integration.html
This appendix provides instructions on adapting REST requests for use with cURL. cURL is a command line tool for transfering data across various protocols, including HTTP, and supports multiple platforms such as Linux, Windows, Mac OS and Solaris. Most Linux distributions include cURL as a package.
Installing cURL
A Red Hat Enterprise Linux user installs cURL with the following terminal command: yum install curl
For other platforms, seek installation instructions on the cURL website (http://curl.haxx.se/).
Using cURL
cURL uses a command line interface to send requests to a HTTP server. Integrating a request requires the following command syntax: Usage: curl [options]
uri
The
uri
refers to target HTTP address to send the request. This is a location on your Red Hat Enterprise Virtualization Manager host within the API entry point path (/api
). cURL options
- -X
COMMAND
, --requestCOMMAND
- The request command to use. In the context of the REST API, use
GET
,POST
,PUT
orDELETE
.Example:-X GET
- -H
LINE
, --headerLINE
- HTTP header to include with the request. Use multiple header options if more than one header is required.Example:
-H "Accept: application/xml" -H "Content-Type: application/xml"
- -u
USERNAME:PASSWORD
, --userUSERNAME:PASSWORD
- The username and password of the Red Hat Enterprise Virtualization user. This attribute acts as a convenient replacement for the
Authorization:
header.Example:-u admin@internal:p@55w0rd!
- --cacert
CERTIFICATE
- The location of the certificate file for SSL communication to the REST API. The certificate file is saved locally on the client machine. Use the
-k
attribute to bypass SSL. See Chapter 2, Authentication and Security for more information on obtaining a certificate.Example:--cacert ~/Certificates/rhevm.cer
- -d
BODY
, --dataBODY
- The body to send for requests. Use with
POST
,PUT
andDELETE
requests. Ensure to specify theContent-Type: application/xml
header if a body exists in the request.Example:-d "<cdrom><file id='rhel-server-6.0-x86_64-dvd.iso'/></cdrom>"
Examples
The following examples show how to adapt REST requests to cURL command syntax: Example A.1. GET
request
The following
GET
request lists the virtual machines in the vms
collection. Note that a GET
request does not contain a body. GET /api/vms HTTP/1.1 Accept: application/xml
Adapt the method (
GET
), header (Accept: application/xml
) and URI (https://[RHEVM-Host]
:8443/api/vms
) into the following cURL command: $ curl -X GET -H "Accept: application/xml" -u [USER:PASS] --cacert [CERT] https://[RHEVM-Host]:8443/api/vms
An XML representation of the
vms
collection displays. Example A.2. POST
request
The following
POST
request creates a virtual machine in the vms
collection. Note that a POST
request requires a body. POST /api/vms HTTP/1.1 Accept: application/xml Content-type: application/xml <vm> <name>vm1</name> <cluster> <name>default</name> </cluster> <template> <name>Blank</name> </template> <memory>536870912</memory> <os> <boot dev="hd"/> </os> </vm>
Adapt the method (
POST
), headers (Accept: application/xml
and Content-type: application/xml
), URI (https://[RHEVM-Host]
:8443/api/vms
) and request body into the following cURL command: $ curl -X POST -H "Accept: application/xml" -H "Content-type: application/xml" -u [USER:PASS] --cacert [CERT] -d "<vm><name>vm1</name><cluster><name>default</name></cluster><template><name>Blank</name></template><memory>536870912</memory><os><boot dev='hd'/></os></vm>" https://[RHEVM-Host]:8443/api/vms
The REST API creates a new virtual machine and displays an XML representation of the resource.
Example A.3. PUT
request
The following
PUT
request updates the memory of a virtual machine resource. Note that a PUT
request requires a body. PUT /api/vms/082c794b-771f-452f-83c9-b2b5a19c0399 HTTP/1.1 Accept: application/xml Content-type: application/xml <vm> <memory>1073741824</memory> </vm>
Adapt the method (
PUT
), headers (Accept: application/xml
and Content-type: application/xml
), URI (https://[RHEVM-Host]
:8443/api/vms/082c794b-771f-452f-83c9-b2b5a19c0399
) and request body into the following cURL command: $ curl -X PUT -H "Accept: application/xml" -H "Content-type: application/xml" -u [USER:PASS] --cacert [CERT] -d "<vm><memory>1073741824</memory></vm>" https://[RHEVM-Host]:8443//api/vms/082c794b-771f-452f-83c9-b2b5a19c039
The REST API updates the virtual machine with a new memory configuration.
Example A.4. DELETE
request
The following
DELETE
request removes a virtual machine resource. DELETE /api/vms/082c794b-771f-452f-83c9-b2b5a19c0399 HTTP/1.1
Adapt the method (
DELETE
) and URI (https://[RHEVM-Host]
:8443/api/vms/082c794b-771f-452f-83c9-b2b5a19c0399
) into the following cURL command: $ curl -X DELETE -u [USER:PASS] --cacert [CERT] https://[RHEVM-Host]:8443//api/vms/082c794b-771f-452f-83c9-b2b5a19c039
The REST API removes the virtual machine. Note the
Accept: application/xml
request header is optional due to the empty result of DELETE
requests. Example A.5. DELETE
request with body
The following
DELETE
request force removes a virtual machine resource as indicated with the optional body. DELETE /api/vms/082c794b-771f-452f-83c9-b2b5a19c0399 HTTP/1.1 Accept: application/xml Content-type: application/xml <action> <force>true</force> </action>
Adapt the method (
DELETE
), headers (Accept: application/xml
and Content-type: application/xml
), URI (https://[RHEVM-Host]
:8443/api/vms/082c794b-771f-452f-83c9-b2b5a19c0399
) and request body into the following cURL command: $ curl -X DELETE -H "Accept: application/xml" -H "Content-type: application/xml" -u[USER:PASS]
--cacert[CERT]
-d "<action><force>true</force></action>" https://[RHEVM-Host]
:8443//api/vms/082c794b-771f-452f-83c9-b2b5a19c039
The REST API force removes the virtual machine.
https://fxtrade.oanda.com/community/forex-forum/topic/54006025/?page=1#post-9924845
https://fxtrade.oanda.com/community/forex-forum/topic/54006025/?page=1#post-9924845
http://wiki.apache.org/solr/UpdateJSON
http://projects.oucs.ox.ac.uk/rails/howtos/rest/handout.pdf
반응형