Code Examples

Code examples in cURL, Python, Go, and JavaScript

The following examples show the POST request to submit an article to the protected http://localhost:8080/api/v1/articles endpoint in different programming languages:

  • cURL
  • Python: The Python example uses the standard requests library for making HTTP requests in Python. To install requests, use the command pip install requests.
  • Go: The Go example uses the built-in net/http package in Go to make HTTP requests.
  • JavaScript: The JavaScript example uses the built-in fetch function in JavaScript, which is available in all modern browsers and in Node.js environments.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
      # Set your JWT token here
    TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNzBiODcxNmQtOGZjYi00NTUyLWJkMDktNTU3OTMxOGZmZmQzIiwiZXhwIjoxNzU0NDE1OTI3LCJpYXQiOjE3NTQzMjk1Mjd9.dUUlE22W8TuSX5od3MOMC0wx3X2xYf51v04E_aCbm2s"
    
    curl --location 'http://localhost:8080/api/v1/articles' \
    --header "Authorization: Bearer $TOKEN" \
    --header 'Content-Type: application/json' \
    --data '{
        "url": "https://www.example.com/a-great-article"
    }'
      
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    import requests
    import json
    
    # Set your JWT token here
    token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNzBiODcxNmQtOGZjYi00NTUyLWJkMDktNTU3OTMxOGZmZmQzIiwiZXhwIjoxNzU0NDE1OTI3LCJpYXQiOjE3NTQzMjk1Mjd9.dUUlE22W8TuSX5od3MOMC0wx3X2xYf51v04E_aCbm2s"
    
    url = "http://localhost:8080/api/v1/articles"
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    data = {
        "URL": "https://www.example.com/a-great-article"
    }
    
    response = requests.post(url, headers=headers, data=json.dumps(data))
    
    print(f"Status Code: {response.status_code}")
    print(f"Response Body: {response.json()}")
      
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    
    package main
    
    import (
    	"bytes"
    	"encoding/json"
    	"fmt"
    	"net/http"
    )
    
    func main() {
    	// Set your JWT token here
    	token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNzBiODcxNmQtOGZjYi00NTUyLWJkMDktNTU3OTMxOGZmZmQzIiwiZXhwIjoxNzU0NDE1OTI3LCJpYXQiOjE3NTQzMjk1Mjd9.dUUlE22W8TuSX5od3MOMC0wx3X2xYf51v04E_aCbm2s"
    
    	url := "http://localhost:8080/api/v1/articles"
    
    	// Create the request body
    	data := map[string]string{"url": "https://www.example.com/a-great-article"}
    	jsonData, _ := json.Marshal(data)
    
    	// Create the HTTP request
    	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
    	if err != nil {
    		fmt.Println("Error creating request:", err)
    		return
    	}
    
    	// Set the headers
    	req.Header.Set("Authorization", "Bearer "+token)
    	req.Header.Set("Content-Type", "application/json")
    
    	// Send the request
    	client := &http.Client{}
    	resp, err := client.Do(req)
    	if err != nil {
    		fmt.Println("Error sending request:", err)
    		return
    	}
    	defer resp.Body.Close()
    
    	fmt.Println("Status Code:", resp.StatusCode)
    }
     
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    // Set your JWT token here
    const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNzBiODcxNmQtOGZjYi00NTUyLWJkMDktNTU3OTMxOGZmZmQzIiwiZXhwIjoxNzU0NDE1OTI3LCJpYXQiOjE3NTQzMjk1Mjd9.dUUlE22W8TuSX5od3MOMC0wx3X2xYf51v04E_aCbm2s";
    
    const url = "http://localhost:8080/api/v1/articles";
    const data = {
        url: "https://www.example.com/a-great-article"
    };
    
    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${token}`
        },
        body: JSON.stringify(data)
    })
    .then(response => {
        // Check if the request was successful
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
        return response.json();
    })
    .then(data => {
        console.log("Success:", data);
    })
    .catch(error => {
        console.error("Error:", error);
    });