RestAssuredResponseImpl: getting body as String results in NPE

1,733 views
Skip to first unread message

bjello

unread,
Sep 28, 2012, 2:54:43 PM9/28/12
to REST assured
Hi!

I'm new to RestAssured (using 1.6.2). When I tried to read the content
of the response's body as a String, I got a NullPointerException.
However, during debugging the REST server logs successful messages and
the content length of the response is as expected, the result is
there, I just can not read it. Then I tried to read the body as
InputStream and it worked. So, is there a problem with my code?

Response response = RestAssured.get(path);
String token = response.getBody().asString();

I also tried to use print() or prettyPrint() but I got the same
results.
The exception gets thrown in RestAssuredResponseImpl in

private String convertStreamToString(InputStream is) throws
IOException {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
Reader reader;
try {
reader = new BufferedReader(new InputStreamReader(is,
findCharset()));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
reader.close();
}
return writer.toString();
}

When I reach finally, the reader is already null again:
java.lang.NullPointerException: Cannot invoke method close() on null
object
Any idea why I can't use asString or prettyPrint?

Cheers!
Bjello

Johan Haleby

unread,
Sep 29, 2012, 6:17:25 AM9/29/12
to rest-a...@googlegroups.com
Hmm it sounds like a bug indeed but it's very hard to tell what's going on. Can you provide some details on the data (body) your resource returns?

/Johan

devnu...@gmx.de

unread,
Sep 30, 2012, 6:40:39 PM9/30/12
to rest-a...@googlegroups.com
Hi again,

> Hmm it sounds like a bug indeed but it's very hard to tell what's going
> on.
> Can you provide some details on the data (body) your resource returns?

I tested two methods. The first one returns a String of length 32 plus newline:
return Response.ok( token + "\n" ).build();
The annotation on the method:
@Produces("text/plain;charset=UTF-8")


The second method returns a list in json of users from a database:
@Produces("application/json;charset=UTF-8")
....
SelectQuery query = new SelectQuery(cls);
List<?> result = (List<?>) context.performQuery(query);
return Response.ok(result).build();

I'm using tapestry-resteasy and cayenne as OR-Mapper.

With both methods I got a NullPointerException when I tried to use asString(). For now I'm using asInputStream() and Apache IOUtils to convert the stream to a String and it works.


-Bjello




Johan Haleby

unread,
Oct 1, 2012, 3:05:18 AM10/1/12
to rest-a...@googlegroups.com
Hmm I can't reproduce this. Could you create a small sample that demonstrates the issue?

Regards,
/Johan

bjello

unread,
Oct 1, 2012, 8:55:24 AM10/1/12
to rest-a...@googlegroups.com
I created a simple tapestry project with maven in eclipse. Hope this works for you.

Right click on the RATests.java in src/test/java and choose run as Junit Test. This starts a Jetty server, runs 4 tests and stops the server.
The tests aren't really tests, the methods just try to read the response. The first two tests should work, the other two produce the NullPointerException.
I'm not sure if you'd need the RunJettyRun plugin for eclipse.

Cheers!
testproject.zip

Johan Haleby

unread,
Oct 1, 2012, 9:14:04 AM10/1/12
to rest-a...@googlegroups.com
Thanks for your help! I'm not using Eclipse (I'm using Intellij) but I'll see if I can get it to work (hopefully later this evening).

Regards,
/Johan

bjello

unread,
Oct 1, 2012, 10:05:00 AM10/1/12
to rest-a...@googlegroups.com
I tried to run it without eclipse:
> cd testproject
> mvn jetty:run

Entering http://localhost:8080/testproject/rest/testtoken in firefox produced the fake token
and http://localhost:8080/testproject/rest/testuser?email=te...@example.org&pw=password produced the user info in json.
I just don't know how to run junit tests with maven but I could try some more on Wednesday.

Regards,
Bjello

Johan Haleby

unread,
Oct 1, 2012, 10:15:55 AM10/1/12
to rest-a...@googlegroups.com
I have the project up and running in Intellij (no problems at all) now and I also get the same error that you get. I'll try to investigate it soon.

Regards,
/Johan

Johan Haleby

unread,
Oct 1, 2012, 10:36:46 AM10/1/12
to rest-a...@googlegroups.com
Alright I've fixed the bug. The problem was that your server returns an unrecognized charset in the content-type header response (which made RA throw NPE). You actually return "UTF-8" (in quotation) and not UTF-8. I'm not sure if this is valid according to the HTTP spec. If it is I'll fix it so that it's unescaped automatically. So what I've fixed is the NPE bug so that you now get the real exception instead. 

Regards,
/Johan

bjello

unread,
Oct 2, 2012, 10:50:35 AM10/2/12
to rest-a...@googlegroups.com

You actually return "UTF-8" (in quotation) and not UTF-8. I'm not sure if this is valid according to the HTTP spec.

I think you're right, it should be 
Content-Type: text/plain; charset=UTF-8
without quotes. I haven't figured out yet how to make Resteasy to not add them. When I don't set the encoding at all, i. e. I change the annotation in MyResource.java from
@Produces("text/plain;charset=UTF-8") to
@Produces("text/plain") it works, there's no exception.

To get the RestAssured version with the "real" exception I guess I should check out the source from the git repository, right?

Anyway, thanks a lot for the fast help. :-)

Regards,
Bjello

Johan Haleby

unread,
Oct 2, 2012, 12:30:41 PM10/2/12
to rest-a...@googlegroups.com
If Resteasy add quotations then perhaps it's valid? In that case I should fix that in RA but I'm not sure if it's correct. Reading https://bugzilla.mozilla.org/show_bug.cgi?id=700589 I interpret it that it's not allowed to use quotations around the charset, what do you think?

Yes you would have to build from trunk for now :/

/Johan 

Johan Haleby

unread,
Oct 2, 2012, 3:35:53 PM10/2/12
to rest-a...@googlegroups.com
I've deployed the latest snapshot to sonatype if you want to try it out. Depend on version 1.6.3-SNAPSHOT after having added the following repo:

<repositories>
        <repository>
            <id>sonatype</id>
            <snapshots />
        </repository>
</repositories>

Regards,
/Johan

bjello

unread,
Oct 2, 2012, 7:53:11 PM10/2/12
to rest-a...@googlegroups.com
Thanks for the snapshot.


If Resteasy add quotations then perhaps it's valid? In that case I should fix that in RA but I'm not sure if it's correct. Reading https://bugzilla.mozilla.org/show_bug.cgi?id=700589 I interpret it that it's not allowed to use quotations around the charset, what do you think?

From RFC 2616 HTTP (http://www.ietf.org/rfc/rfc2616.txt)

14.17 Content-Type [...]
Content-Type   = "Content-Type" ":" media-type

Media types are defined in section 3.7. An example of the field is
    Content-Type: text/html; charset=ISO-8859-4

There are no quotes in this example.

From section 3.7 Media Types:
    media-type     = type "/" subtype *( ";" parameter )

From 3.6:
Parameters are in the form of attribute/value pairs. parameter = attribute "=" value

They say nothing about quotes for the value here. Hmm.

In 3.4 Character Sets:
[...] HTTP character sets are identified by case-insensitive tokens. The complete set of tokens is defined by the IANA Character Set registry [19]. charset = token

I looked around some more on the IANA website but I couldn't find anything explicit about quotes for the charset token. The charsets and their names are listed without quotes on http://www.iana.org/assignments/character-sets.
In RFC 2046 (MIME) there's another example for the type text/plain:

4.1.2.  Charset Parameter
A critical parameter that may be specified in the Content-Type field
for "text/plain" data is the character set.  This is specified with a
"charset" parameter, as in:

    Content-type: text/plain; charset=iso-8859-1

Again, no quotes. Well, I'm no expert but I'd say RestEasy is wrong and there's nothing to fix in RA. :-)

Regards
Bjello

Johan Haleby

unread,
Oct 3, 2012, 1:54:04 AM10/3/12
to rest-a...@googlegroups.com
Thanks for your investigation, really helpful! :) I'll leave it as it is then. 

If you have any other problems or questions in the future just shoot!

Regards,
/Johan

bjello

unread,
Oct 4, 2012, 5:47:36 AM10/4/12
to rest-a...@googlegroups.com
Just for the record: I tried it with tomcat instead of jetty and the response's content type was text/plain;charset=UTF-8 or application/json;charset=UTF-8, there weren't any quotes. So RestEasy isn't doing anything wrong either, it looks like it's actually Jetty.

Regards,
Bjello


Johan Haleby

unread,
Oct 4, 2012, 6:26:40 AM10/4/12
to rest-a...@googlegroups.com
Alright, thanks for sharing.

/Johan

Ben Cooney

unread,
Jun 2, 2015, 12:09:05 AM6/2/15
to rest-a...@googlegroups.com
Hi 

Sorry to bring this old thread back. (Is it better to start a new conversation?) 

I've also hit the problem with the system under test returning a charset with double-quotes around the token/value. -- application/json; charset="utf-8"

I am testing some services through a gateway. The gateway is sending the problematic ContentType header and unfortunately the gateway is not something we can alter. 

Is there a way I can override the responses ContentType header in my rest-assured code?

Thanks,
Ben

Johan Haleby

unread,
Jun 2, 2015, 2:26:00 AM6/2/15
to rest-a...@googlegroups.com
You could create a Filter and change the content type from there.

/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages