Writing data is a diverse situation. There are many methods used for creating, modifying, or deleting data, but the Retrofit API by default only supports the POST, PUT, and DELETE methods. In this video, learn how in most cases this is all you will need.
- [Instructor] DELETE is pretty straightforward, so let's get that out of the way first. This method should do exactly what it says it does, you delete the resource at the target address. It is important to note that it does not require a body to be present in the request, just like the GET and HEAD methods. The POST and PUT methods however, are used to modify or create data on the server. Aside from a difference in verb, these two methods are fundamentally different from the GET and HEAD methods because a message body is usually expected.
The message body is then used to describe the desired state of an existing object, a new object that needs to be created, or the parameters needed for a procedure to execute. For example, when creating a new comment on a gist on GitHub, which we will do later on in the course, the request might look like this, and the response might look like this. Now PUT and POST are not interchangeable, they don't have the same kind of relationship that GET and HEAD do.
The PUT method is used to set the state of a specific record. If the record does not exist it will be created, if it does exist it might be updated depending on how the API is designed. The POST method is used to create a record and let the server define its ID, or execute some predefined procedure. Think of it like, you are putting data into a location, or you are posting a message to the server.
- REST API basics
- Methods for reading and writing data
- Defining data models with Retrofit
- Implementing an API interface
- Getting data with a Retrofit call
- Authorizing requests
- Putting, deleting, and posting data
- Customizing the data converter
- Working with the request and response headers