How to handle plain text server response?
Asked Answered
R

2

14

I am new to AngularJS, and building an app that will interact with a server. The server has a REST API, but responds to some methods with plain text, and to others with JSON. I have implemented a simple http request method using AngularJS' $resource service.

However, when the server response is plain text, the response in AngularJS is an object with one entry for each character in the response word. How can I get around this (in a good way)? Ideally, I would like to be able to tell my service when to expect plain text and when to expect JSON, and get a nicely formatted response in both cases.

Rabinowitz answered 26/7, 2013 at 17:10 Comment(1)
I have exactly the same problem. According to REST, the returning object of a POST operation should be the URI (or like in my case, an ID) of the newly created object. Angular fails here in the $resource helper by assuming all responses to be JSON. I guess a custom response handler could solve this.. Will experiment with that. Keep you posted ;)Nanete
B
21

$resource is a convenience wrapper for working with Restful objects. It'll automatically try to parse as JSON and populate the object based on the $resource definition.

You are much better off using the $http service for non restful resources.

This is a lower level API that doesn't have such overbearing object mapping.

e.g.

$http({method: "GET", url: "/myTextDocURL"})
  .success(function(data){ 
      // data should be text string here (only if the server response is text/plain)
  }
);
Becht answered 26/7, 2013 at 18:35 Comment(3)
Why for non restful resources??Wholly
Thank you! I really needed to use plain text and didn't think the issue was because I was using $resource. Changed to $http and it worked. (transformResponse: undefined)Stave
I'm amazed this post is still useful to people tbh! Glad it saved you a headache.Becht
M
16

According to the documentation you specify a custom action for a resource that can override the default behaviour which is to to convert the response from json to a javascript object. The 'data' param of the transformResponse function will contain your text payload.

In this case the transformResponse method returns an object containing the string rather than just the string itself because otherwise it would STILL try to convert the string to an array.

    var Stub = $resource('/files/:filename', {}, {'getText': {
        transformResponse: function(data, headersGetter, status) {
            return {content: data};
        }
    }});

To use the resource call your custom getText() action rather than plain old get():

    Stub.getText({'filename': 'someFile.txt'}, function(response) {
        console.info("Content of someFile.txt = " . response.content);
    });

This is an old post but I figured it deserved an new answer.

Marlysmarmaduke answered 8/5, 2015 at 8:31 Comment(2)
this worked and in my case i was returning json data as plain/text (on purpose) but $http was still turning it into an object vs a plain string, so this method worked best, plus i can still use my resource for other functions. ThanksZilber
THIS IS THE CORRECT ANSWERLout

© 2022 - 2024 — McMap. All rights reserved.