Search code examples
javahttpjaxbhttp-headersxmlhttprequest

How to fire HTTP post with content type XML along with query parameters?


I am trying to fire a POST request. The data I am sending is XML format (I am doing this via JAXB), but I have to send along a request parameter. My problem is regarding the Content-Type (I've added some comments in the code). Which one should I use ?

See below my code :

       private V callAndGetResponse(K request, Class<K> requestClassType, Class<V> responseClassType) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(requestClassType, responseClassType);

    Marshaller marshaller = jaxbContext.createMarshaller();
    // set properties on marshaller
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
    marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", String.format(XML_HEADER_FORMAT, dtd));
    marshaller.marshal(request, System.out);

    URL wsUrl = new URL(primaryEndpointUrl);
    HttpURLConnection connection = openAndPrepareConnection(wsUrl);
    tryToMarshallWsRequestToOutputStream(request, marshaller, connection);

    printDebug(connection);

    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object response = unmarshaller.unmarshal(connection.getInputStream());

    // cleanup
    connection.disconnect();
    return responseClassType.cast(response);
}

private HttpURLConnection openAndPrepareConnection(URL wsUrl) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) wsUrl.openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Accept", "application/xml");
    connection.setRequestProperty("Accept-Charset", charset);
    // Both types ? Doesn't work
    connection.setRequestProperty("Content-Type", "application/xml;application/x-www-form-urlencoded");
    // Only app/xml ? it seems that query param is not added to request
    connection.setRequestProperty("Content-Type", "application/xml");
    // Only app/query param ? it seems that xml is not added to request
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    return connection;
}

private void tryToMarshallWsRequestToOutputStream(K request, Marshaller jaxbForRequestMarshaller,
        HttpURLConnection connection) throws JAXBException, IOException {
    OutputStream outputStream = null;
    try {
        outputStream = connection.getOutputStream();
        jaxbForRequestMarshaller.marshal(request, outputStream);
        addQueryParameters(outputStream);
    }
    finally {
        tryToClose(outputStream);
    }
}

private void addQueryParameters(OutputStream outputStream) throws IOException {
    String value = "none";
    String query = String.format("xmlmsg=%s", URLEncoder.encode(value, charset));
    outputStream.write(query.getBytes(charset));
}

protected void tryToClose(OutputStream outputStream) throws IOException {
    if (outputStream != null) {
        outputStream.close();
    }
}

Thanks a lot!


Solution

  • Ok, there's two different issues that you're dealing with:

    1. Content-Type of the body. In this case, if your body is an XML body, then it should be:

      Content-Type: application/xml; charset="utf-8"
      
    2. The fact that your query parameters are being stripped off (or ignored). This is a more complicated problem. When a form is submitted via an HTTP Post, it is often sent as query string params, and this looks like what might be happening. Can you get a wire trace of what's being sent? It may be that you'll need to parse the parameters off yourself instead of trying to parse them as a form body.

    If you need to send two different types of data, you may consider sending something like a multipart form, or sending one body type, and adding extra data as a header.