AICollection Help

POST

The POST method is used to submit data to be processed to a specified resource. It often results in a change in state or side effects on the server. Unlike GET, POST requests can include a body containing the data to be sent to the server.

Basic Syntax

POST /path/to/resource HTTP/1.1 Host: example.com Content-Type: application/json { "key1": "value1", "key2": "value2" }

Key Characteristics

  • Non-idempotent: Multiple identical POST requests may have different effects.

  • Not Safe: POST requests can change the state of the server.

  • Cacheable: Responses to POST requests are not typically cached.

Parameters

  • Request Body: POST requests often include a body containing the data to be sent to the server.

    { "key1": "value1", "key2": "value2" }
  • Headers: POST requests can include headers to specify the content type and other metadata.

    Content-Type: application/json

Examples

  1. Basic POST Request

    POST /users HTTP/1.1 Host: example.com Content-Type: application/json { "name": "John Doe", "email": "john.doe@example.com" }

    This request creates a new user with the specified name and email.

  2. POST Request with Form Data

    POST /submit-form HTTP/1.1 Host: example.com Content-Type: application/x-www-form-urlencoded name=John+Doe&email=john.doe%40example.com

    This request submits form data to the server.

  3. POST Request with Headers

    POST /upload HTTP/1.1 Host: example.com Content-Type: multipart/form-data Authorization: Bearer token --boundary Content-Disposition: form-data; name="file"; filename="example.txt" Content-Type: text/plain This is the content of the file. --boundary--

    This request uploads a file to the server with an authorization header.

Conclusion

The POST method is essential for submitting data to a server. Understanding its characteristics and how to use request bodies and headers allows you to effectively send data and interact with web APIs.

Last modified: 05 December 2024