Using the Curl command to implement restful interface operations

Source: Internet
Author: User
Tags session id

Curl is a convenient rest client, and it's easy to complete the needs of many rest API tests, and even if it's a rest API that needs to be logged in or certified, it can be tested using the Curl instruction to send HTTP GET, POST, PUT, DELETE, you can also change the HTTP header to satisfy the specific conditions required to use the rest API.

Curl has a lot of parameters, and this is a list of the things that are commonly used when testing rest:

-X/--Request[GET|POST|PUT|DELETE| ...] Using the specifiedHTTP methodOutHTTP request-H/--Header set request header-< Span class= "PLN" >i/--include  show response< Span class= "pun" > header-d/-- data  set  http parameters - v/--verbose  more messages < Span class= "pun" >-u/--user  user account, password -b/--cookie cookie    

The parameters of the Linux command line are often, the same function will often have two functions exactly the same parameters, one is a shorter argument, the front is usually used - (one - ) to guide the symbol, the other longer parameters, usually use -- (two - ) to guide the symbol

Use it in curl to explain

  -X, --Request COMMANDSpecifyRequest command toUse --Resolve HOST:PORT:ADDRESSForceResolve of HOST:port to ADDRESS --retry NUM retry request NUM times iftransient problems occur --retry-delay SECONDS when Retrying wait this Many seconds between each --retry-max-time SECONDS retry only within this Period                 

The parameters -X --request are the same as the two functions, so the use ex:curl -X POST http://www.example.com/ curl --request POST http://www.example.com/ is equal to the function

How to use Get/post/put/delete

-X adds HTTP method to the back side,

curl -X GET "http://www.rest.com/api/users"curl -X POST "http://www.rest.com/api/users"curl -X PUT "http://www.rest.com/api/users"curl -X DELETE "http://www.rest.com/api/users"

URL to add an argument can also, without an extension can also, if there are non-pure English text or number of characters, without the quotation may be problematic, if it is the URL of the Web, also add the quotation

HEADER

Messages added to the HTTP header

curl -v -i -H "Content-Type: application/json" http://www.example.com/users
HTTP Parameter

The HTTP parameters can be added directly to the query string of the URL, or can be threaded with the take-in -d parameters & , or use multiple-d

# use ' & ' to string multiple parameters curl-x post-d "Param1=value1&param2=value2" # can also use multiple '-d ', Effect curl-x post-d "param1=value1"-D "par Am2=value2 "Curl-x post-d" Param1=a 0space "     #" a space "url encode the blank character will be encoded as '%20 ' as" A%20space ", after the code can directly use Curl-x POS T-d "Param1=a%20space"
Post JSON format information

As time required to send the request parameter and Json,request parameter can be added to the URL, JSON information into -d the parameters, and then use the single reference to the JSON information is included (if the JSON content is a single reference,- D to use the double-reference package), the header to add "Content-type:application/json" and "Accept:application/json"

1 Curl http://Www.example.com?modifier=kent-X put-i-H "Content-type:application/json"-H " Accept:application/json "-d ' {" Boolean ": false," foo ":" Bar "} '2 # not added "accept:application/ JSON" can also be 3 Curl http://Www.example.com?modifier=kent-X put-i-H" Content-type:application/json "-d ' {" Boolean ": false," foo ":" Bar "} '

Service that needs to be certified or logged in before it can be used

Many services need to be logged in or confirmed before they can access their API service, and depending on the conditions required by the service, Curl is able to pass through the Cookie,session or join the session KEY,API key or the token that is recognized in the header to achieve the effect.

Session Example:

Back end If the user logged in with the session, the backend will pass a session ID to the front end, the front end needs to place this session ID in the header of each requests with the back end, and the session The ID knowledge Front end is the session, in order to achieve the effect of the session

' http://www.rest.com/api/users ' ' sessionid:1234567890987654321 '

Cookie Example

If a cookie is used, after the confirmation, the backend will return a cookie to the file, and when the URL needs to be accessed, the -b cookie_file cookie can be used in the request.

1 # Store Cookies 2 curl-i-x post-d username=kent-d password=kent123-c  ~/cookie.txt  http://  Www.rest.com/auth34"accept:application/json" - X get-b ~/cookie.txt http://WWW.REST.COM/USERS/1
File.
' [Email Protected]/users/kent/my_file.txt ' ' Name=a_file_name '

This is through the HTTP multipart POST on the information, -F is the use of HTTP query parameter, specify the file location of the parameters to add@

HTTP Basic Authentication (HTTP base certificate)

If the Web site is an HTTP basic confirmation, you can use --user username:password log in

Curl-i--user Kent:secret http://Www.rest.com/api/foo '    

If you fail, you'll be401 Unauthorized

1http/1.1 401Unauthorized2server:apache-coyote/1.13x-content-type-Options:nosniff4X-xss-protection:1; Mode=Block5Cache-control:no-cache, No-store, max-age=0, must-revalidate6pragma:no-Cache7Expires:08x-frame-Options:deny9Www-authenticate:basic realm="Realm"Tencontent-type:text/html;charset=utf-8 Onecontent-Language:en AContent-length:1022 -Date:thu, theMay the  .: +: theGmt

When the evidence passes, the response200 OK

1http/1.1  $OK2server:apache-coyote/1.13x-content-type-Options:nosniff4X-xss-protection:1; Mode=Block5Cache-control:no-cache, No-store, max-age=0, must-revalidate6pragma:no-Cache7Expires:08x-frame-Options:deny9set-cookie:jsessionid=a75066dcc816ce31d8f69255deb6c30b; path=/mdserver/; HttpOnlyTencontent-type:application/json;charset=utf-8 Onetransfer-encoding:chunked ADate:thu, theMay the  .: -: OneGmt

You can save the certified cookie and reuse it.

Curl-i--user Kent:secret http://Www.rest.com/api/foo '-C ~/cookies.txt

Cookies that are stored prior to login may not be recognized every time.

Curl-i  http://www.rest.com/api/foo '-B ~/cookies.txt

Using the Curl command to implement restful interface operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.