Posting data to REST WS
===

I've got problem with creating client with REST WS using Java SE. I'm connected properly. I can get data from server, but I can't POST it. I've searched everywhere but I can't solve this problem. It shows me 415 error code all the time.

Please tell me what I'm doing wrong or maybe how can I do it other way. I just want to work it.


That's example from API.

    POST /qcbin/rest/domains/DOMAIN_NAME/projects/PROJECT_NAME/defects HTTP/1.1
    Content-Type: application/xml
    Accept: application/xml
    Cookie: QCSession=xxx; LWSSO_COOKIE_KEY=xxx
    <Entity Type="defect">
    <Fields>
         <Field Name="detected-by">
              <Value>henry_tilney</Value>
         </Field>
         <Field Name="creation-time">
              <Value>2010-03-02</Value>
         </Field>
         <Field Name="severity">
              <Value>2-Medium</Value>
         </Field>
         <Field Name="name">
              <Value>Returned value not does not match
                            value in database.</Value>
         </Field>
    </Fields>
    </Entity>


My code is:

    public boolean postEntity2(String webPage) throws Exception{
        URL url = new URL(webPage);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type: ", "application/xml");
        conn.setRequestProperty("Accept: ", "application/xml");
        conn.setRequestProperty("Cookie: ", "QCSession="+session+"; LWSSO_COOKIE_KEY="+c.getCookie());
        conn.setDoInput(true);
        conn.setDoOutput(true);
        OutputStream output = conn.getOutputStream();
        String xml="<Entity Type=\"defect\"><Fields><Field Name=\"detected-by\"><Value>henry_tilney</Value></Field>"
                + "<Field Name=\"creation-time\"><Value>2010-03-02</Value></Field><Field Name=\"severity\">"
                + "<Value>2-Medium</Value></Field><Field Name=\"name\"><Value>some value</Value></Field></Fields></Entity>";
        output.write(xml.getBytes());
        conn.connect();
        System.out.println("response code: "+conn.getResponseCode());
        Constants.showHeaders(conn);
        if(conn.getResponseCode()==201){ return true; }
        Constants.showErrorStream(conn);
        return false;
    }


And I get this:

    Error code: 415.

    Header name: null
    Header value: HTTP/1.1 415 Unsupported Media Type
    
    Header name: Server
    Header value: Apache-Coyote/1.1
    
    Header name: X-Powered-By
    Header value: Servlet 2.5; JBoss-5.0/JBossWeb-2.1
    
    Header name: Content-Type
    Header value: application/xml
    
    Header name: Transfer-Encoding
    Header value: chunked
    
    Header name: Date
    Header value: Fri, 20 Jul 2012 07:41:14 GMT

And errorStream:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><QCRestException><Id>qccore.general-error</Id><Title>Unsupported Media Type</Title><StackTrace>javax.ws.rs.WebApplicationException&#xD;
    	at org.apache.wink.server.internal.registry.ResourceRegistry.filterDispatchMethods(ResourceRegistry.java:468)&#xD;
    	at org.apache.wink.server.internal.registry.ResourceRegistry.findMethod(ResourceRegistry.java:352)&#xD;
    	at org.apache.wink.server.internal.handlers.FindResourceMethodHandler.handleResourceMethod(FindResourceMethodHandler.java:138)&#xD;
    	at org.apache.wink.server.internal.handlers.FindResourceMethodHandler.handleRequest(FindResourceMethodHandler.java:65)&#xD;
    	at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)&#xD;
    	at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)&#xD;
    	at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:61)&#xD;
    	at org.apache.wink.server.internal.handlers.FindRootResourceHandler.handleRequest(FindRootResourceHandler.java:93)&#xD;
    	at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)&#xD;
    	at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)&#xD;
    	at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:61)&#xD;
    	at org.apache.wink.server.internal.handlers.HeadMethodHandler.handleRequest(HeadMethodHandler.java:53)&#xD;
    	at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)&#xD;
    	at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)&#xD;
    	at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:61)&#xD;
    	at org.apache.wink.server.internal.handlers.OptionsMethodHandler.handleRequest(OptionsMethodHandler.java:46)&#xD;
    	at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)&#xD;
    	at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)&#xD;
    	at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:61)&#xD;
    	at org.apache.wink.server.internal.handlers.SearchResultHandler.handleRequest(SearchResultHandler.java:33)&#xD;
    	at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)&#xD;
    	at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)&#xD;
    	at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:61)&#xD;
    	at org.apache.wink.server.handlers.AbstractHandlersChain.run(AbstractHandlersChain.java:48)&#xD;
    	at org.apache.wink.server.internal.RequestProcessor.handleRequestWithoutFaultBarrier(RequestProcessor.java:163)&#xD;
    	at org.apache.wink.server.internal.RequestProcessor.handleRequest(RequestProcessor.java:137)&#xD;
    	at org.apache.wink.server.internal.servlet.RestServlet.service(RestServlet.java:116)&#xD;
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)&#xD;
    	at org.hp.qc.web.restapi.RESTApiFilter.doFilter(RESTApiFilter.java:65)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)&#xD;
    	at org.hp.qc.web.restapi.performance.PerformanceMonitorRESTFilter.doFilter(PerformanceMonitorRESTFilter.java:45)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)&#xD;
    	at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)&#xD;
    	at com.hp.sw.bto.ast.security.lwsso.authenticator.springSecurity.SpringSecurity2LWSSOIntegrationFilter.doFilter(SpringSecurity2LWSSOIntegrationFilter.java:182)&#xD;
    	at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)&#xD;
    	at org.hp.qc.web.restapi.sessionmanagement.ImplicitSessionFilter.doFilter(ImplicitSessionFilter.java:54)&#xD;
    	at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)&#xD;
    	at org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)&#xD;
    	at org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)&#xD;
    	at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)&#xD;
    	at com.hp.sw.bto.ast.security.lwsso.authenticator.springSecurity.LWSSO2SpringSecurityIntegrationFilter.doFilter(LWSSO2SpringSecurityIntegrationFilter.java:299)&#xD;
    	at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)&#xD;
    	at org.springframework.security.ui.ExceptionTranslationFilter.doFilterHttp(ExceptionTranslationFilter.java:101)&#xD;
    	at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)&#xD;
    	at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)&#xD;
    	at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)&#xD;
    	at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)&#xD;
    	at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)&#xD;
    	at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)&#xD;
    	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)&#xD;
    	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)&#xD;
    	at com.mercury.optane.core.state.ServerStatusFilter.doFilter(ServerStatusFilter.java:33)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)&#xD;
    	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)&#xD;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)&#xD;
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)&#xD;
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)&#xD;
    	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)&#xD;
    	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)&#xD;
    	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)&#xD;
    	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)&#xD;
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)&#xD;
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)&#xD;
    	at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)&#xD;
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)&#xD;
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)&#xD;
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)&#xD;
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)&#xD;
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)&#xD;
    	at java.lang.Thread.run(Thread.java:619)&#xD;
    </StackTrace></QCRestException>