0

I'm using TaskOptions.payload(String ) method to write a small JSON object into a POST taskqueue.

But how do I get it out to read in the servlet when executing the task queue?

Eurig Jones
  • 8,226
  • 7
  • 52
  • 74

2 Answers2

2

If you are using servlets than you must implement a doPost(..) method, where you get the request body and parse it as JSON: HttpServletRequest get JSON POST data

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
1

Here's what I did in the end. Ran this code in the doPost()..

import org.codehaus.jackson.map.ObjectMapper;
import my.own.PayloadObject;

...

private static final ObjectMapper MAPPER = new ObjectMapper();

...

private PayloadObject getPayload(HttpServletRequest req) throws IOException
{
    InputStream inputStream = req.getInputStream();
    ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();

    int length;
    byte[] buffer = new byte[1024];

    while ((length = inputStream.read(buffer)) >= 0)
        byteArrayStream.write(buffer, 0, length);

    if (byteArrayStream.size() > 0)
        return MAPPER.readValue(byteArrayStream.toByteArray(), PayloadObject.class);

    return null;
}
Eurig Jones
  • 8,226
  • 7
  • 52
  • 74