views:

58

answers:

2

Hi,

I would like to reate a POST request to a given address, let it for example be

http://staging.myproject.com/products.xml?product[title]=TitleTest&product[content]=TestContent&product[price]=12.3&tags=aaa,bbb

for POST requests I've created an universal method:

private String postMethod(String url, HashMap<String, String> headers, String encodedAuthorizationString) throws HttpException, IOException {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(url);
    post.setRequestHeader("Authorization", encodedAuthorizationString);
    if(headers != null && !headers.isEmpty()){
        for(Entry<String, String> entry : headers.entrySet()){
            post.setRequestHeader(new Header(entry.getKey(), entry.getValue()));
        }
    }
    client.executeMethod(post);
    String responseFromPost = post.getResponseBodyAsString();
    post.releaseConnection();
    return responseFromPost;
}

where headers represents pairs (key, value), for example ("product[title]", "TitleTest"). I tried to use the method by calling postMethod("http://staging.myproject.com.products.xml", headers, "xxx"); where headers included pairs

("product[title]", "TitleTest"),

("product[content]", "TestContent"),

(product[price], "12.3"),

("tags", "aaa,bbb")

but the server returned an error message.

Does anyone know how to parse the address

http://staging.myproject.com/products.xml?product[title]=TitleTest&amp;product[content]=TestContent&amp;product[price]=12.3&amp;tags=aaa,bbb

in order to use it with the method above? Which part is url? Are parameters set correctly?

Thank you.

+1  A: 

You seem to be confusing URL query parameters, such as product[price]=12.3 with HTTP request headers. Using setRequestHeader() is meant to set the HTTP request headers, which are meta-data associated with any HTTP request.

In order to set the query parameters, you should append them to the url, after a '?' and UrlEncoded, as in your example url.

Avi
+2  A: 

I've found a problem:

private String postMethod(String url, HashMap<String, String> headers, String encodedAuthorizationString) throws HttpException, IOException {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
post.setRequestHeader("Authorization", encodedAuthorizationString);
if(headers != null && !headers.isEmpty()){
    for(Entry<String, String> entry : headers.entrySet()){
        //post.setRequestHeader(new Header(entry.getKey(), entry.getValue()));
        //in the old code parameters were set as headers (the line above is replaced with the line below)
        post.addParameter(new Header(entry.getKey(), entry.getValue()));
    }
}
client.executeMethod(post);
String responseFromPost = post.getResponseBodyAsString();
post.releaseConnection();
return responseFromPost;

}

url = http://staging.myproject.com/products.xml

parameters:

("product[title]", "TitleTest"),

("product[content]", "TestContent"),

(product[price], "12.3"),

("tags", "aaa,bbb")
niko