SoFunction
Updated on 2025-04-14

Java implements curl to call interface method with parameter

usecurlWhen calling an interface with parameters, the method of passing parameters depends on the HTTP method and interface requirements. Here are examples of different scenarios:

1. GET request (parameters are in URL query string)

curl "/api?name=John&age=30"

Notice: If the parameter contains special characters (such as spaces), it is required-GCooperate--data-urlencodeAutomatic encoding:

curl -G /api \
  --data-urlencode "query=hello world" \
  --data-urlencode "page=1"

The generated URL:/api?query=hello%20world&page=1

2. POST request (form data format)

curl -X POST \
  -d "name=John" \
  -d "age=30" \
  /api

Or merge parameters:

curl -X POST -d "name=John&age=30" /api

Default headerContent-Type: application/x-www-form-urlencoded

3. POST request (JSON format)

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"name":"John", "age":30}' \
  /api

4. Mix URL parameters and request body (POST)

curl -X POST \
  "/api?category=tech" \
  -d "title=Hello&content=World"

5. Upload file (Multipart form)

curl -X POST \
  -F "file=@/path/to/" \
  -F "description=My File" \
  /upload

6. PUT/PATCH request

curl -X PUT \
  -H "Content-Type: application/json" \
  -d '{"id": 1, "status": "active"}' \
  /api/resource/1

Key Option Description

  • -X <METHOD>: Specify HTTP method (such asGETPOSTPUT)。
  • -d <DATA>: Send request body data (automatically set to POST method unless using-XCoverage).
  • -H <HEADER>: Add request header (such asContent-Type)。
  • -G: Forced to-dConvert the parameter to URL query string (commonly used for GET).
  • --data-urlencode: Automatically URL encoding of parameters.
  • -F <FIELD=DATA>: Send multipart/form-data data (file upload).

Select the appropriate method and data format according to the interface document.

This is the article about Java implementing curl calls with parameter interface methods. For more related Java curl calls with parameter interface content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!