No methods in http response object

http methods
httpresponse django
http status codes
response object python
http request methods
response object javascript
http options method example
http methods wiki

Object returned by $http.get has no methods. Example: I have my class model

export class Lab {

    constructor(
        public id: number,
        public name: string,
        public description: string,
        public isActive: boolean,
        public classes: Classes[]
    ) { }

    isActive(lab: Lab) {
        return this.isActive;
    }
}

in my service I call http fetching lab

getLab(labId: number) {
    return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId).toPromise();
}

and when I get this in some component, method isActive is undefined, so call

lab.isActive();

throws exception. Is there any clean solution for this?

The server just returns data formed with properties from the defined object. It doesn't actually create an instance of the object.

Try something like this:

this.lab = Object.assign(new Lab(), this.retrievedLab)

Where this.retrievedLab is the data returned from the server.

This should create the object and then copy any of the retrieved properties into it.

Request and response objects | Django documentation, The “patch” methods ensure that other values, e.g. added by a middleware, are not removed. HTTP header fields cannot contain newlines  The res object represents the HTTP response that an Express app sends when it gets an HTTP request. Following is the list of few properties associated with response object. This property holds a reference to the instance of the express application that is using the middleware. Boolean property that indicates if the app sent HTTP headers for the

In your get call service you can do as @Deborahk mentioned

getLab(labId: number) {
return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId)
           .map(res => Object.assign(new Lab(), res))
           .toPromise();
 }

Response, Response.redirected Read only: Indicates whether or not the response with the response with Response.headers for values of the HTTP Trailer header. Methods. Response.clone(): Creates a clone of a Response object. The requests.Response() Object contains the server's response to the HTTP request. Properties and Methods. Property/Method

You can also have a class and extend it like this:

getLab(labId: number) {
    return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId)
    .pipe(Lab.serializeResponseMap())
    .toPromise();
}

class definition:

export class Lab extends SerializableMap {
    static instanceType = Lab;

    constructor(
        public id: number,
        public name: string,
        public description: string,
        public isActive: boolean,
        public classes: Classes[]
    ) { super(); }

    isActive(lab: Lab) {
        return this.isActive;
    }
}


class SerializableMap {
  static instanceType: any;

  static serializeResponseMap(): any {
    const createInstance = (r) => {
      return Object.assign(new this.instanceType(), r);
    };

    return map((respValue: any) => {
      if (Array.isArray(respValue)) {
        return respValue.map(r => createInstance(r));
      }
      return createInstance(respValue);
    });
  }
}

HTTP request methods, Requests using GET should only retrieve data. HEAD: The HEAD method asks for a response identical to that of a GET request, but without the  You can use the Response object to send output to the client. The Response object defines the following methods. Sets the HTML header name to value. Adds a string to the end of the Web server log entry for this request. Writes the given information to the current HTTP output without any character-set conversion. Erases any buffered HTML output.

This version is inspired by @Matjaz Hirsman response (thanks!), with added deep cloning. Also: it resembles the Decorator pattern more than Serialization (or actually Deserialization).

getLab(labId: number) {
    return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId)
    .pipe(Lab.decoratingMap())
    .toPromise();
}

Classes:

export class Lab extends Decorable {
    static instanceType = Lab;

    constructor(
        public id: number,
        public name: string,
        public description: string,
        public isActive: boolean,
        public classes: Classes[]
    ) { super(); }

    isActive(lab: Lab) {
        return this.isActive;
    }
}


class Decorable {
  static instanceType: any;

  /**
   * Convert single entity into fully-fledged object
   * @param source js object
   * @return fully-fledged HalResource
   */
  static decorateSingle(source: any) {
    const target = new this.instanceType();
    for (const key in target) {
      if (source[key]) {
        if (target[key] && typeof target[key] === 'object') {
          target[key] = Object.assign(target[key], source[key]);
        } else {
          target[key] = source[key];
        }
      }
    }
    return target;
  };

  /**
   * Convert entity or array of entities into fully-fledged objects
   * @param response js object (or objects)
   * @return fully-fledged object (or objects)
   */
  static decorate(response: any) {
    if (Array.isArray(response)) {
      return response.map(element => this.decorateSingle(element))
    } else {
      return this.decorateSingle(response);
    }
  }

  /**
   * Rx Map operator decorating the JS objects into fully-fledged objects
   */
  static decoratingMap() {
    return map((response: any) => this.decorate(response));
  }
}

HTTP Methods – REST API Verbs – REST API Tutorial, In this case, either HTTP response code 200 (OK) or 204 (No Content) is the appropriate response status. Responses to this method are not cacheable, unless the  The methods and properties of the HttpResponse class are exposed through the Response property of the HttpApplication, HttpContext, Page, and UserControl classes. The following methods of the HttpResponse class are supported only in post back scenarios and not in asynchronous post back scenarios: BinaryWrite. Clear. ClearContent. ClearHeaders. Close. End. Flush

HttpResponse (Java SE 9 & JDK 9 ), public abstract class HttpResponse<T> extends Object Methods are provided in this class for accessing the response headers, and response body. Response​  The Response interface of the Fetch API represents the response to a request.. You can create a new Response object using the Response.Response() constructor, but you are more likely to encounter a Response object being returned as the result of another API operation—for example, a service worker Fetchevent.respondWith, or a simple GlobalFetch.fetch().

HttpResponse Class (System.Web), Lastly, the code calls the Flush method to send the buffered response to the In code, the HttpResponse object is referred to by the keyword Response . of the HttpResponse class are supported only in post back scenarios and not in  I guess your HTTP Response is a JSON containing a PriceTag? The issue is that you want to create a PriceTag object. You can just convert the json to a PriceTag by type casting, but then it won't be a real PriceTag object. The way we have resolved this is:

HTTP request and response, A HTTP request object always has a HTTP response object associated with it, and the If the query string is not present in the HTTP request, it is an empty string. from the HTTPRequest object by using the get method with the key BODY :. Sr.No. Header & Description; 1: Allow. This header specifies the request methods (GET, POST, etc.) that the server supports. 2: Cache-Control. This header specifies the circumstances in which the response document can safely be cached.

Comments
  • Typing the response object does not do any casting or conversion. It's just a hint to the compiler. If you want to convert the plain object you get from the JSON, you need to write that code.
  • Possible duplicate of Angular: Typescript casting JSON response as object model not working
  • Or stackoverflow.com/questions/46839214/…, or stackoverflow.com/questions/45145385/…, or ...
  • It's not duplicated, this problem is about calling method after a return from backend. This question was really useful.
  • you saved my day, I was about to do a lot of manual labor to make this copy. Thank you