Add samples on the README to give a quick feedback on Hurl syntax.

This commit is contained in:
jcamiel 2020-09-04 21:09:30 +02:00
parent 82505b4ac7
commit ee8b856502

223
README.md
View File

@ -8,12 +8,10 @@
# What's Hurl?
Hurl is a command line tool and a simple plain text format for describing an HTTP session.
Hurl is used in command lines or scripts to run HTTP sessions. Hurl can performs requests, capture values
and evaluate queries on headers and body response. Hurl is very versatile: it can be used to get HTTP data and
also to test HTTP sessions.
Hurl is a command line tool that performs HTTP requests defined in a simple plain text format.
It can perform requests, capture values and evaluate queries on headers and body response.
Hurl is very versatile: it can be used for both fetching data and testing HTTP sessions.
```hurl
# Get home:
@ -53,7 +51,7 @@ HTTP/1.1 200
xpath "normalize-space(//head/title)" equals "Hello world!"
```
and is well adapted for REST/json apis
It is well adapted for REST/json apis
```hurl
POST https://api.example.net/tests
@ -96,6 +94,219 @@ Visit the [Hurl web site](https://hurl.dev) to find out how to install and use H
- [Samples](https://hurl.dev/docs/samples.html)
- [File Format](https://hurl.dev/docs/entry.html)
# Samples
## Getting Data
A simple GET:
```hurl
GET https://example.net
```
[Doc](https://hurl.dev/docs/request.html#method)
A simple GET with headers:
```hurl
GET https://example.net/news
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:70.0) Gecko/20100101 Firefox/70.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
```
[Doc](https://hurl.dev/docs/request.html#headers)
### Query Params
```hurl
GET https://example.net/news
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:70.0) Gecko/20100101 Firefox/70.0
[QueryStringParams]
order: newest
search: something to search
count: 100
```
Or:
```hurl
GET https://example.net/news?order=newest&search=something%20to%20search&count=100
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:70.0) Gecko/20100101 Firefox/70.0
```
[Doc](https://hurl.dev/docs/request.html#query-parameters)
## Sending Data
### Sending HTML Form Datas
```hurl
POST https://example.net/contact
[FormParams]
default: false
token: {{token}}
email: john.doe@rookie.org
number: 33611223344
```
[Doc](https://hurl.dev/docs/request.html#form-parameters)
### Sending Multipart Form Datas
```hurl
POST https://example.net/upload
[MultipartFormData]
field1: value1
field2: file,example.txt;
# On can specify the file content type:
field3: file,example.zip; application/zip
```
[Doc](https://hurl.dev/docs/request.html#multipart-form-data)
### Posting a JSON Body
With an inline JSON:
```hurl
POST https://api.example.net/tests
{
"id": "456",
"evaluate": true
}
```
[Doc](https://hurl.dev/docs/request.html#json-body)
With a local file:
```hurl
POST https://api.example.net/tests
Content-Type: application/json
file,data.json;
```
[Doc](https://hurl.dev/docs/request.html#file-body)
### Templating a JSON/XML Body
Using templates with [JSON body](https://hurl.dev/docs/request.html#json-body) or [XML body](https://hurl.dev/docs/request.html#xml-body)
is not currently supported in Hurl. Besides, you can use templates in [raw string body](https://hurl.dev/docs/request.html#raw-string-body)
with variables to send a JSON or XML body:
~~~hurl
PUT https://api.example.net/hits
Content-Type: application/json
```
{
"key0": "{{a_string}}",
"key1": {{a_bool}},
"key2": {{a_null}},
"key3": {{a_number}}
}
```
~~~
Variables can be initialized via command line:
```bash
$ hurl --variable key0=apple --variable key1=true --variable key2=null --variable key3=42 test.hurl
```
Resulting in a PUT request with the following JSON body:
```
{
"key0": "apple",
"key1": true,
"key2": null,
"key3": 42
}
```
[Doc](https://hurl.dev/docs/request.html#raw-string-body)
## Testing Response
### Testing REST Apis
```hurl
GET https//example.org/order
screencapability: low
HTTP/1.1 200
[Asserts]
jsonpath "$.validated" equals true
jsonpath "$.userInfo.firstName" equals "Franck"
jsonpath "$.userInfo.lastName" equals "Herbert"
jsonpath "$.hasDevice" equals false
jsonpath "$.links" countEquals 12
```
[Doc](https://hurl.dev/docs/asserting-response.html#jsonpath-assert)
### Testing HTML Response
```hurl
GET https://example.com
HTTP/1.1 200
Content-Type: text/html; charset=UTF-8
[Asserts]
xpath "string(/html/head/title)" contains "Example" # Check title
xpath "count(//p)" equals 2 # Check the number of p
xpath "//p" countEquals 2 # Similar assert for p
xpath "boolean(count(//h2))" equals false # Check there is no h2
xpath "//h2" not exists # Similar assert for h2
```
[Doc](https://hurl.dev/docs/asserting-response.html#xpath-assert)
## Others
### Using SOAP Apis
```hurl
POST https://example.net/InStock
Content-Type: application/soap+xml; charset=utf-8
SOAPAction: "http://www.w3.org/2003/05/soap-envelope"
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:m="http://www.example.org">
<soap:Header></soap:Header>
<soap:Body>
<m:GetStockPrice>
<m:StockName>GOOG</m:StockName>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200
```
[Doc](https://hurl.dev/docs/request.html#xml-body)
### Capturing and Using a CSRF Token
```hurl
GET https://example.net
HTTP/* 200
[Captures]
csrf_token: xpath "string(//meta[@name='_csrf_token']/@content)"
POST https://example.net/login?user=toto&password=1234
X-CSRF-TOKEN: {{csrf_token}}
HTTP/* 302
```
[Doc](https://hurl.dev/docs/capturing-response.html#xpath-capture)
# Feedbacks