brl8
brl8

Reputation: 646

Sending post attributes and xml data with a post request?

New to java, GWT and interacting with APIs. I have what I hope is a simple question.

I have successfully interacted with a REST API using the following curl command:

curl -d "OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=xxxxxxxxxxx&INPUT_DATA=<?xml version=%221.0%22 encoding=%22utf-8%22?><Operation><Details><requester>Me</requester><subject>Test</subject><description>Testing curl input</description></Details></Operation>" http://xx.xx.xx.xx/sdpapi/request/ 

Now, from a tutorial, I have the following code that I am hoping will post a request to the remote server just like the curl command above.

What I am trying to figure out (with no love from google) is how I pass the OPERATION_NAME, TECHNICIAN_KEY and INPUT_DATA parameters in when I am sending the URL. Any suggestions, tutorials, etc. will be appreciated.

The following is from my server side implementation interface:

@Override
public String postToRemoteServer(String serviceUrl)
        throws HelpDeskTestException {

    try {
        //dividing url into host: http://some.server
        //path: a/path/in/it
        //and parameters: this=that&those=others

        int hostStart= serviceUrl.indexOf("//");

        int pathStart= serviceUrl.substring(hostStart + 2).indexOf("/");

        int parameterStart= serviceUrl.substring(hostStart + 2 + pathStart).indexOf("?");

        final String serverHost= serviceUrl.substring(0, hostStart + pathStart + 2);

        final String serverPath= serviceUrl.substring(hostStart + 3, 
                hostStart + pathStart + 2 + parameterStart);

        final String serverParameters= serviceUrl.substring(hostStart + pathStart + 3 + parameterStart);

        final  URL url = new URL(serverHost);

        final URLConnection connection= url.openConnection();
        connection.setDoOutput(true);

        final OutputStreamWriter out= new OutputStreamWriter(connection.getOutputStream());

        final BufferedReader in= new BufferedReader(new InputStreamReader(
                connection.getInputStream()));

        out.write("POST " + serverPath + "\r\n");
        out.write("Host: " + serverHost + "\r\n");
        out.write("Accept-Encoding: identity\r\n");
        out.write("Connection: close\r\n");
        out.write("Content-Type: application/x-www-form-urlencoded\r\n");
        out.write("Content-Length: " + serverParameters.length() + "\r\n\r\n" +
            serverParameters + "\r\n");


        String result = "";
        String inputLine;

        while ((inputLine=in.readLine()) != null) {
            result+= inputLine;
        }

        in.close();
        out.close();

        return result;

    }  catch (final Exception e) {
        throw new HelpDeskTestException();

    }

Upvotes: 1

Views: 1351

Answers (1)

Michael Easter
Michael Easter

Reputation: 24498

Consider using this library: Apache HttpClient. Here is an example of making a POST request with it.

Upvotes: 1

Related Questions