Http response through service sends content (mime) type in text/html but not application/json

1.8k Views Asked by At

My application using Oauth for basecam api. I am trying to get Httpresponse into json format but it revert into plain html (text/html) content-type. so there is no method to parse HTML content and get the token from basecamp. This is not homework but a small R&D to quick start Oauth protocol. as am new to oauth.

//HERE -> final String JSON_CONTENT = "application/json"
String contentType = OAuthConstants.JSON_CONTENT;

    if (response.getEntity().getContentType() != null) {
        contentType = response.getEntity().getContentType().getValue();
        //BELOW -> getting contentType is in "text/html; utf-8
        System.out.println(response.getEntity().getContentType().getValue()); //text/html; charset=utf-8
    }

    if (contentType.contains(OAuthConstants.JSON_CONTENT)) {
        return handleJsonResponse(response);
    } else 
        if (contentType.contains(OAuthConstants.URL_ENCODED_CONTENT)) {
          return handleURLEncodedResponse(response);
        } else 
            if (contentType.contains(OAuthConstants.XML_CONTENT)) {
              return handleXMLResponse(response);
            }   
            else {
                // Unsupported Content type
                throw new RuntimeException(
                        "Cannot handle "
                                + contentType
                                + " content type. Supported content types include JSON, XML and URLEncoded");
            }

So above lines explain very well that control won't come is json, xml or url_encoded if-else. Si either i need to parse text/html into json or xml response or i have to create another method name handleHtmlResponse(). what way it would be continent to get contentType.

1

There are 1 best solutions below

1
On

After the response is set with all the data(header, body ...), commit it by calling ServletResponse#flushBuffer.