Retrofit - Send a simple POST Request | Android Studio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
uh hello there and welcome back to my new video so with this video we're going to continue our retrofit tutorial series and i'm going to show you how to send a post request this time so far we have used only get request to receive a response from the server and now we are actually going to send or post some data to the server unfortunately because we are using a fake rest api we will not be able to see the actual data on the server side instead we are going to receive back response with the exact same data we sent to the server using post request and for those of you who are not familiar with post request basically a post request is used to send data to the server to create or update a resource and the data sent to the server with a post request is stored in the request body of the http request and not in the url also i'm going to show you how to use the form url encoded annotation and the requests made with this annotation will have a application form url encoded meme type instead of a json like the regular post request and the field names and the values will be utf-8 encoded before being uri encoded okay so this is our fake rest api website and this is the actual endpoint which we are going to use for our post request so uh let's open up our android studio project and uh we're going to use the same project from before in this retrofit tutorial series so you can also download this project in this video description as well okay so let's create a new function here which will which will represent the post request and let's annotate that function with a post annotation okay let's name this function push post and this function will have only one parameter annotated with a body annotation and the body annotation means that this parameter will be actually used in a request body of this post request so we have specified this post object and that is our model class from before okay and this function will return a post wrapped in a response okay so inside our repository we're going to create another function named push post and it will take a post as a parameter and it will return a post wrapped in response and here we're going to call the retrofit instance and then call this push post okay and inside our main view model we're going to create a new function as well so just pass this post as a parameter and here we are going to use a kotlin code routines or a viewmodelscope to actually call this pushpost from our repository and we will uh store the result of this response inside this my response mutable live data object and we're going to observe that mutable live data from our main activity so inside our main activity uh instead of this get custom posts we're going to call this push post function and we need to pass a post so let's create a post object and here let's pass uh four parameters so for the user id and the id i'm going to pass number two for the title i'm going to write step jason the name of my youtube channel and in the body i'm going to say android developer okay and just pass this post uh and here we're going to observe this my response uh mutable live data object okay and inside this if block uh i'm going to just log the response okay so i'm going to call the body and convert that to string and here i'm going to uh call a code so we can see the the response code and of course the message as well okay so now uh i have opened the wire shark and this is an application used to uh analyze your network traffic so uh i have filtered this wire truck to only look for for requests from this specific host and now when i run the app i'm going to open up this locket and i will see the results so here i can see the actual results which we sent to the server as you can see the user id is number two but the id is number 101 and that is because this server fake rest api server does not accept the id instead it generates this id by itself automatically and we can also see this response code 201 and this response code indicates that the request has succeeded and has led to the creation of a resource and because our rest api is a fake that's why we cannot actually see the data on the server instead we have received that data back to uh as our response okay so that works perfectly fine and now inside of inside our wireshark we can see the actual request we sent to the server and uh down below we can check some more informations so as you can see let's open this up and we can see the content type of this request is application.json and we can see also some of more informations from the request header and if we scroll down below we can find the actual data we have sent to this server so let's scroll down below and you will see this javascript object notation application json so that means that those uh values or those data or this data is sent in a format of json so retrofit has converted our data to a json and then it sends that to a server okay so uh the next thing uh we're going to create another function another post request and uh this time we will add the new annotation name the form url encoded okay so this requests made with this annotation will have application form url encoded meme type so if you are wondering what is the difference between a json and form url encoded meme type well if you don't specify this form url encoded annotation and the retrofit library will use a json as a meme type by default and it will send a request to a server where data will be in a json format and if you specify this form url encoded annotation then the retrofit will send a request to a server where where data will be sent in a format of a key and value separated by an ampersand symbol okay and here as a parameter we need to remove this body and we need to add uh four different field annotations with four different parameters so this field annotation will accept the key and this parameter will represent the value okay so here i'm just setting this user id id title and a body and we're going to get those values dynamically so from the repository let's create another function named push post number two and this function will take four different parameters so the user id of the type integer idea with the type of integer title and the body of the type of string and basically the main difference between a post and get request is that in a get request the data is sent in a url and in a post request the data is sent in the request body okay so now that we have created this function inside our repository let's open up our main view model and here let's create a new function as well so let's rename this to push post number two and let's add four different parameters and here let's uh call this push post number two and let's pass those four parameters as well okay so let's open up our main activity and here let's uh call a push post number two instead and here i'm going to pass those four parameters uh directly okay okay i'm going to say stereo and in the body i'm going to say android all right and now let's run our application and let's open up a wireshark so here you will see a new request once the application starts and we can see this uh content type of form url encoded and that's the difference between the last request we send which is in the json format and down below we can also see the data we have sent and now we can see this uh data where it says html form url encoded and basically this data is sent in a key and value pair separated with ampersand symbol and that's it so you now saw how you can send a post request in a json format and also how you can send the post request in a form of uh form url encoded meme type and uh that's all pretty much straightforward so that will be all for this video uh thanks for watching please like this video if you find it helpful of course and see the next if one die i'm a legend when they lay me down
Info
Channel: Stevdza-San
Views: 54,905
Rating: undefined out of 5
Keywords: retrofit, http, client, android, library, get, request, response, result, connect, to, internet, web, post, put, patch, delete, read, receive, data, from, send, how to, guide, tutorial, app, https, parse, json, kotlin, object, gson, converter, moshi, server, respond, display, in, builder, model, repository, class, instance, viewmodel, exception, url, uri, base, update, resource, wireshark, network, traffic, analyse, create, code, 201, FormUrlEncoded, form, encoded, annotation, mime, type, content, format
Id: ok697I1j1aE
Channel Id: undefined
Length: 9min 12sec (552 seconds)
Published: Sat Aug 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.