Log in or register to post comments

[VWS Java w/ Spring] 401 Unauthorized in POST/PUT methods

January 24, 2014 - 11:20am #1
I'm using the VWC api with Java & Spring, but I have a problem:
When call a GET or DELETE service the authorization header is correctly setted, but when I try to use a POST or PUT service always have like an answer "401 Unauthorized". I guess the error is the md5 compression.
These is  the Java example provided by Vuforia :
private String contentMD5(HttpEntityEnclosingRequestBase httpMethod) {
    ByteArrayOutputStream requestOutputStream = new ByteArrayOutputStream();
    try {
    } catch (IOException e) {
        System.out.println("ERROR: IOException caught when writing Content MD5 hash");
    return DigestUtils.md5Hex(requestOutputStream.toByteArray()).toLowerCase();


And it's my code:


private static String encodeMD5(Jsonizable body) throws JsonGenerationException, JsonMappingException, IOException {

    return DigestUtils.md5Hex(SerializationUtils.serialize(body.getAsJSON())).toLowerCase();

public String getAsJSON() throws JsonGenerationException, JsonMappingException, IOException {

    ObjectMapper mapper = new ObjectMapper();

    return mapper.writeValueAsString(this);



* Serialize the given object to a byte array.
* @param object the object to serialize
* @return an array of bytes representing the object in a portable fashion
public static byte[] serialize(Object object) {

	if (object == null) {

		return null;


	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	try {

		ObjectOutputStream oos = new ObjectOutputStream(baos);




	catch (IOException ex) {

		throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);


	return baos.toByteArray();



Is there another way of retrieve more information from the error?



[VWS Java w/ Spring] 401 Unauthorized in POST/PUT methods

January 24, 2014 - 1:37pm #2

The response includes a JSON body w/ more info, but it's not going to tell you the specific cause of the authentication error.

Have you confirmed that the result of the getAsJSON method is equivalent to getEntity()? - the hashing is sensitive to the string format and white space, so be sure that what your methods aren't changing the request body.

Log in or register to post comments