Define an HTTP POST Endpoint for a Node.js Server with Swagger

Will Button
InstructorWill Button

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 4 months ago

In this lesson, you will learn how to define HTTP POST operations for your Todo API server using Swagger. Creating the endpoint, passing the todo in the request body, and parsing the todo are all demonstrated.

Instructor: [0:00] We're also going to have a post method supported on the root endpoint. By posting to that is how we're going to add a new ToDo. We need to define our operation ID, which is going to be called add ToDo.

[0:14] Once we start writing code, you're going to see where this operation ID comes into play. It produces application/json. Then here we have parameters. Our getOperation() didn't have any parameters.

[0:30] In order to post a new ToDo, we're obviously going to have to be able to accept some parameters. We're going to find those in the body. The name that we'll be looking for, for the parameters in the body, is going to be ToDo. We'll specify that it is required.

[0:48] Then once again, our schema is going to use that dollar sign ref and then point to the ToDo definition. We're going to have some responses. We'll have a 200 response.

[1:06] Then the last thing that we need to specify is the x-swagger-router-controller and that's going to be add ToDo as well. Again, you'll see why that is and how that works whenever we get into writing the code for this.

Raphi
Raphi
~ 6 years ago

You demonstrate how to send an object (i.e. a Todo) in a POST request, by using a schema and $ref to the definition.

How do you describe, in the YAML, just a simple string in the POST body, rather than a schema?

(Pardon me if you cover this in a later video)

Will Button
Will Buttoninstructor
~ 6 years ago

Hi Raphi, This should work for you:

      responses:
        '200':
          description: sample description
          schema:
            type: string