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.

1
  • 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 ;) Commented Jun 15, 2014 at 22:15

2 Answers 2

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) } ); 
Sign up to request clarification or add additional context in comments.

3 Comments

Why for non restful resources??
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)
I'm amazed this post is still useful to people tbh! Glad it saved you a headache.
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.

2 Comments

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. Thanks
THIS IS THE CORRECT ANSWER

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.