In the Linux system, how to initiate a POST/GET request

In Linux systems, you can use the command line tools `curl` or `wget` to send POST requests. Both tools are very commonly used command line tools that can send HTTP requests directly through the command line.

1. Send a POST request using `curl`:

curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://example.com/api/endpoint

Explanation:
- `-X POST`: Specifies the request method as POST.
- `-H "Content-Type: application/json"`: Specifies that the Content-Type in the request header is in JSON format.
- `-d '{"key1":"value1", "key2":"value2"}'`: Specify the data body of the POST request. Here, data in JSON format is used as an example.
- `http://example.com/api/endpoint`: URL to send POST request.

2. Use `wget` to send a POST request:

The `wget` command uses GET requests by default, but you can send POST requests by using the `--post-data` parameter.

wget --method=POST --header="Content-Type: application/json" --body-data '{"key1":"value1", "key2":"value2"}' http://example.com/api/endpoint

Explanation:
- `--method=POST`: Specifies the request method as POST.
- `--header="Content-Type: application/json"`: Specifies that the Content-Type in the request header is in JSON format.
- `--body-data '{"key1":"value1", "key2":"value2"}'`: Specify the data body of the POST request. Here, data in JSON format is used as an example.
- `http://example.com/api/endpoint`: URL to send POST request.

Please note that the URL, request header and data body in the above examples are only examples and need to be adjusted according to your specific needs in actual applications. At the same time, when using `curl` or `wget` to send a POST request, make sure that the target server can handle the POST request correctly and respond with the correct result.

Guess you like

Origin blog.csdn.net/lizhao1226/article/details/131938409