Skip to content

Requests

Sending test requests to APIs.


Introduction

In this tutorial, you'll learn how to use curl HTTPIe to send test requests to APIs.

This tutorial assumes that you followed the following tutorials:

By the end of this tutorial, you'll have learned how to:

  • Use curl or HTTPIe for sending API requests

Before you begin

Before getting started, make sure you have the following:


1. Sending API requests

In the last part of this tutorial, you will use curl or HTTPIe to interact with the deployed Customers API.

Please replace $GATEWAY-URL with the URL of your API Gateway and XXX with your token!

1.1 curl

curl is s widely-used by developers for quickly testing REST APIs in the command line.

The following command will perform the request and output the response body:

curl -v --location https://$GATEWAY-URL/customers/customers -H 'accept: application/json' -H 'Authorization: Bearer XXX'

Which will show:

curl -v --location https://$GATEWAY-URL/customers/customers -H 'accept: application/json' -H 'Authorization: Bearer XXX'
*   Trying XX.XX.XX.XX:443...
* Connected to $GATEWAY-URL (XX.XX.XX.XX) port 443 (#0)
* ALPN: offers h2
* ALPN: offers http/1.1
*  CAfile: /etc/ssl/cert.pem
*  CApath: none
* [CONN-0-0][CF-SSL] (304) (OUT), TLS handshake, Client hello (1):
* [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, Server hello (2):
* [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, Unknown (8):
* [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, Certificate (11):
* [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, CERT verify (15):
* [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, Finished (20):
* [CONN-0-0][CF-SSL] (304) (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / AEAD-CHACHA20-POLY1305-SHA256
* ALPN: server accepted h2
* Server certificate:
*  subject: CN=*.$GATEWAY-URL
*  start date: Feb 28 11:52:59 2023 GMT
*  expire date: May 29 11:52:58 2023 GMT
*  subjectAltName: host "$GATEWAY-URL" matched cert's "*.$GATEWAY-URL"
*  issuer: C=US; O=Let's Encrypt; CN=R3
*  SSL certificate verify ok.
* Using HTTP2, server supports multiplexing
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* h2h3 [:method: GET]
* h2h3 [:path: /customers/customers]
* h2h3 [:scheme: https]
* h2h3 [:authority: $GATEWAY-URL]
* h2h3 [user-agent: curl/7.87.0]
* h2h3 [accept: */*]
* Using Stream ID: 1 (easy handle 0x12b010a00)
> GET /customers/customers HTTP/2
> Host: $GATEWAY-URL
> user-agent: curl/7.87.0
> accept: */*
>
* Connection state changed (MAX_CONCURRENT_STREAMS == 250)!
< HTTP/2 200
< access-control-allow-credentials: true
< cache-control: no-cache
< content-type: application/json; charset=utf-8
< date: Fri, 31 Mar 2023 13:04:11 GMT
< etag: W/"152-YYUUBmMg0FfHP0BwM9kbIO/ll84"
< expires: -1
< pragma: no-cache
< vary: Origin, Accept-Encoding
< x-content-type-options: nosniff
< x-powered-by: Express
< content-length: 338
<
[
  {
    "id": 1,
    "firstName": "John",
    "lastName": "Doe",
    "points": 100,
    "status": "bronze"
  },
  {
    "id": 2,
    "firstName": "Jane",
    "lastName": "Doe",
    "points": 200,
    "status": "silver"
  },
  {
    "id": 3,
    "firstName": "John",
    "lastName": "Smith",
    "points": 300,
    "status": "gold"
  }
* Connection #0 to host $GATEWAY-URL left intact
]%

1.2 HTTPie

HTTPIe make it painless to test and debug APIs, HTTP servers, and web services.

In the following example, you will run a GET request against the /customers endpoint of the example API.

https -v https://$GATEWAY-URL/customers/customers 'Authorization: Bearer XXX'

This will create the following (colorized and formatted) output:

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Content-Length: 338
Content-Type: application/json; charset=utf-8
Date: Fri, 31 Mar 2023 13:14:12 GMT
Etag: W/"152-YYUUBmMg0FfHP0BwM9kbIO/ll84"
Expires: -1
Pragma: no-cache
Vary: Origin, Accept-Encoding
X-Content-Type-Options: nosniff
X-Powered-By: Express

[
    {
        "firstName": "John",
        "id": 1,
        "lastName": "Doe",
        "points": 100,
        "status": "bronze"
    },
    {
        "firstName": "Jane",
        "id": 2,
        "lastName": "Doe",
        "points": 200,
        "status": "silver"
    },
    {
        "firstName": "John",
        "id": 3,
        "lastName": "Smith",
        "points": 300,
        "status": "gold"
    }
]

Summary

In this tutorial, you learned how to use curl HTTPIe to send test requests to APIs.