usecurl
When 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-G
Cooperate--data-urlencode
Automatic 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 header:Content-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 asGET
,POST
,PUT
)。 -
-d <DATA>
: Send request body data (automatically set to POST method unless using-X
Coverage). -
-H <HEADER>
: Add request header (such asContent-Type
)。 -
-G
: Forced to-d
Convert 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!