So before we go too much further, let's recreate that right, Jason Helper function, because we're
going to be doing this all the time and it might simplify our lives or actually will simplify our lives
so we can help her start.
Go inside the CMD API folder.
Let's create a new function with a receiver of app application, and we'll call this one right, Jason.
And it will take a few arguments, a response, right, so we have something to write to.
But also take the status, which will set the status code for the response, whatever we want to turn
into Jason, and we don't know what that's going to be, so we'll just make that an interface.
And finally, we might want to send some headers.
So I'll create a variable called headers and I'll make that very ADIC so it'll have zero or more headers.
That way it becomes optional and this might return an error.
So very straightforward.
All we're going to do, first of all, is create a variable called out and we'll check for an error
and we'll use Jason martialing debt just like we have before.
And in production.
I would use Marschall, of course, and we're going to marshal data with no prefix but leading tabs
when we format it and we'll check for an error.
If error is not equal to nil, then we'll return the error.
Otherwise, next thing to do is to check to see if headers has been set.
So if the length of headers is greater than zero, then we want to add those headers.
So we'll just say for K for key and V for value is a sign.
The value of range through headers index is zero.
And each time we looked for this we'll just say header at index K for key is equal to V for value.
That way we can set all the headers we need to.
And of course if that option of value was not set, then this just gets skipped.
Now we just write it down header and we set the content type two application, Jason, and we write
the status write header status, which is just an end and status has to have a type up here that's going
to be an aunt.
And we write the letter W, right?
Oh, so we read our data, there's our Jason and I, we return now because there's no error.
Let's give this a comment so we'll say that.
Right.
Jason writes, Arbitrary.
Data out as Jason.
OK, this isn't being used anywhere.
So let's go use it.
Let's go back to our handlers dash API and we'll simply get rid of all of this.
And instead, we'll call and I'm not going to bother with doing anything with the air right now.
We'll call after right Jason and give it to necessary parameters, our response writer.
And in this case, it would be HTP the status, OK, and the pilot.
And I'm not setting any headers so I can leave out that optional fourth parameter and that should actually
work.
So let's try this out.
Let's start our application like start.
And if everything went as expected, we can go use that right.
Jason, everywhere.
We need to go back to our Web browser, will open the JavaScript console, click on login and log in
with any data we want me at here dot com and some password.
And I should get my response over here.
And I do.
Error is false and message success.
So that works as expected and this will greatly simplify our lives as we move on.