user14949876
user14949876

Reputation: 1

How do I pass in values manually for swagger api calling in angular?

{
  "swagger" : "2.0",
  "info" : {
    "description" : "Project API Swagger Documentation",
    "version" : "20-30",
    "title" : "Project API Swagger Documentation",
    "termsOfService" : "",
    "contact" : { }
  },
  "host" : "localhost:9080",
  "basePath" : "/home/project/rest",
  "schemes" : [ "http" ],
  "paths" : {
        "/kmh/dashboardAttributes" : {
          "get" : {
            "tags" : [ "kmh" ],
            "summary" : "Get kmhDashboard Attributes",
            "description" : "Returns attributes for kmh Dashboard",
            "produces" : [ "application/json" ],
            "parameters" : [ {
              "name" : "iv-user",
              "in" : "header",
              "description" : "ID of the user",
              "required" : true,
              "type" : "string"
            } ],

So basically I have a swagger API which references the ID of the user in the header automatically. How do I manually pass in an ID of the user for the API? I am quite confused as to how can I pass in the values in angular so that when calling the API, I am passing in the correct parameters for the api.

** What do I have to do to pass the value in the get request to the api and for the api to take the value from the get request? ** Edit

I am using angular 2.

In my service in angular.

    public getID() : Observable<any> {
        let headers = new Headers({ 'Content-Type': 'application/json'} ); // ... Set content type to JSON      
        let options = new RequestOptions({ headers: headers, withCredentials: true }); // Create a request option

        return this.http.get('/api/services/v1/home/project/kmh/dashboardAttributes' , options ) // ...using get request
            .map(this.extractData)
           .catch(this.handleError);
    }

Upvotes: 0

Views: 834

Answers (1)

Marco
Marco

Reputation: 1093

In your Swagger documentation it states

"parameters" : [ {
  "name" : "iv-user",
  "in" : "header",

So in your header variable, you must add

let headers = new HttpHeaders({ 'Content-Type': 'application/json', "iv-user": "{{my-username}}" }); // ... Set content type to JSON    

And replace {{my-username}} by the username of your user.

I also suggest you to switch from Headers (Fetch API) to HttpHeaders (Angular).

Headers is a class from the Fetch API (not from Angular). I'm not sure it will work correctly with the HttpClient of Angular. You do not need import to use Headers. Unlike HttpHeaders which needs @angular/common/http import and is designed to work with the HttpClient of Angular.

Upvotes: 1

Related Questions