#json-api #http-post #json-file #token #bearer #post-requests #request

app json_pusher

A tool to push JSON data to an endpoint

2 releases

0.1.1 Aug 28, 2024
0.1.0 Aug 28, 2024

#393 in Web programming

22 downloads per month

MIT license

14KB
124 lines

JSON Pusher

JSON Pusher is a simple command-line tool written in Rust that reads a JSON file, iterates over the objects within, and pushes each object to a specified API endpoint. The tool allows you to specify the API URL, request method (e.g., POST, PUT), and an optional bearer token for authorization.

Features

  • Supports POST and PUT requests: Push JSON data using the HTTP method of your choice.
  • Bearer Token Support: Easily include a bearer token for authentication.
  • Iterates Over JSON Objects: Handles JSON arrays by sending each object as a separate request.
  • Cross Platform

Installation

Prerequisites

Ensure you have Rust installed on your machine. If not, you can install it via rustup.

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Cloning the repo

git clone https://github.com/th0jensen/json_pusher.git
cd json_pusher

Building the Project

To build the project, navigate to the project directory and run:

cargo build --release

The compiled binary will be located in the target/release/ directory.

Add to PATH (optional)

cargo install --path .

Usage/Examples

You can run the JSON Pusher using the following command:

json_pusher --json <path_to_json_file> --url <api_endpoint_url> --method <http_method> [--token <path_to_token_file>]

Required Arguments

  • --json (-j): Path to the input JSON file. This file should contain an array of JSON objects.
  • --url (-u): The API endpoint URL where the JSON objects will be sent.
  • --method (-m): The HTTP method to use (e.g., POST, PUT).

Optional Arguments

  • --token (-t): Path to a file containing the bearer token for authorization. If not provided, the requests will be sent without an Authorization header.

Example

json_pusher --json ./data/input.json --url https://api.example.com/endpoint --method POST --token ./data/token.txt

This command will:

  1. Read the JSON objects from input.json.
  2. Iterate over each object and send it as a POST request to https://api.example.com/endpoint.
  3. Include the bearer token from token.txt in the Authorization header of each request.

Sample JSON File

Here’s an example of what your JSON file might look like:

[
    { "id": 1, "name": "Object 1" },
    { "id": 2, "name": "Object 2" }
]

Each object in the array will be sent as a separate request to the specified API endpoint.

Error Handling

  • Unsupported HTTP Method: The tool currently supports only POST and PUT methods. If an unsupported method is specified, the program will exit with an error.
  • File Reading Errors: If the JSON file or token file cannot be read, an error will be displayed, and the program will terminate.

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Dependencies

~6–19MB
~279K SLoC