Published on

DevOps API

Authors
  • Name
    Jackson Chen

How to curl url via proxy server

# Example on curl URL via proxy server
curl -x http://<proxy ip>:<8080>  http://www.vmare.com

curl command line options

https://www.baeldung.com/curl-rest

cURL - client URL

As a web developer or tech enthusiast, understanding HTTP requests and how to interact with APIs is crucial, particularly when it comes to automating requests and debugging. In such scenarios, you might want to execute a quick HTML request from your terminal and this is where cURL, a free command-line tool for data transfer, comes in handy.

API tools like Postman and Insomnia provide an interactive user interface (UI) that allows you to make different forms of requests to URLs, for receiving and processing requests. The cURL command does the same thing, except in your terminal.

# verbose
curl -v http://www.example.com
    # It provide information such as the resolved IP address, port and the headers

# output
By default, curl outputs the response body to standard output. 
Additionally, we can provide the output option to save to a file.
    curl -o out.json http://www.example.com/index.html

# HTTP Methods With curl
cURL -X [METHOD] [URL]

Every HTTP request contains a method. The most commonly used methods are GET, POST, PUT and DELETE

1. GET
This is the default method when making HTTP calls with curl.
    curl -v http://localhost:8082/spring-rest/foos/9

2. POST
We use this method to send data to a receiving service, which means we use the data option.
The simplest way of doing this is to embed the data in the command
    curl -d 'id=9&name=baeldung' http://localhost:8082/spring-rest/foos/new

Alternatively, we can pass a file containing the request body to the data option like this
    curl -d @request.json -H "Content-Type: application/json" 
        http://localhost:8082/spring-rest/foos/new

    -d  # hyphen and lowercase d        which is an alias for --data
        # adding data with the request
Note:
curl adds the following default header to all POST requests:
    Content-Type: application/x-www-form-urlencoded

For instance, if our service expects JSON content-type, 
then we can use the -H option to modify our original POST request:
    curl -d '{"id":9,"name":"baeldung"}' -H 'Content-Type: application/json' 
        http://localhost:8082/spring-rest/foos/new

3. PUT
This method is very similar to POST, but we use it when we want to send a new version of an existing resource. 
In order to do this, we use the -X option

Without any mention of a request method type, curl defaults to using GET; 
therefore, we explicitly mention the method type in the case of PUT:
    curl -d @request.json -H 'Content-Type: application/json' 
    -X PUT http://localhost:8082/spring-rest/foos/9

4. DELETE
Again, we specify that we want to use DELETE by using the -X option:
    curl -X DELETE http://localhost:8082/spring-rest/foos/9

5. Custom Headers
We can replace the default headers or add headers of our own.
. For instance, to change the Host header, we do this:
    curl -H "Host: com.baeldung" http://example.com/

. To switch off the User-Agent header, we put in an empty value:
    curl -H "User-Agent:" http://example.com/

. The most common scenario while testing is changing the Content-Type and Accept header. 
    We just have to prefix each header with the -H option:
        curl -d @request.json -H "Content-Type: application/json" 
        -H "Accept: application/json" http://localhost:8082/spring-rest/foos/new

6. Authentication
A service that requires authentication would send back a 401 – Unauthorized HTTP response code, 
    and an associated WWW-Authenticate header.

. For basic authentication, we can simply embed the username and password combination inside our request using the user option:
    curl --user baeldung:secretPassword http://example.com/

. However, if we want to use OAuth2 for authentication, we first need to get the access_token from our authorization service.
    The service response would contain the access_token:

        The service response would contain the access_token:

        {
        "access_token": "b1094abc0-54a4-3eab-7213-877142c33fh3",
        "token_type": "bearer",
        "refresh_token": "253begef-868c-5d48-92e8-448c2ec4bd91",
        "expires_in": 31234
        }
. Now we can use the token in our Authorization header:
    curl -H "Authorization: Bearer b1094abc0-54a4-3eab-7213-877142c33fh3" http://example.com/