Search code examples
javaresthttprequeststormpath

Stormpath Rest api Java


can someone help me write this in the form of an httpRequest in java. I´ve tried many times and failed. I don´t know why but I simply can´t get it right =(

curl -X POST --user $YOUR_API_KEY_ID:$YOUR_API_KEY_SECRET \
 -H "Accept: application/json" \
 -H "Content-Type: application/json;charset=UTF-8" \
 -d '{
       "favoriteColor": "red",
       "hobby": "Kendo"
     }' \
"https://api.stormpath.com/v1/accounts/cJoiwcorTTmkDDBsf02bAb/customData"

(the custom data has to be in json format)

Any help would be greatly appreciated. =)


Solution

  •     DefaultHttpClient httpClient = new DefaultHttpClient();
    
        HttpPost postRequest = new HttpPost(
                "https://api.stormpath.com/v1/accounts/cJoiwcorTTmkDDBsf02bAb/customData");
    
        String credentials = apiKey.getId() + ":" + apiKey.getSecret();
        postRequest.setHeader("Authorization", "Basic " + Base64.encodeBase64String(credentials.getBytes("UTF-8")));
        postRequest.setHeader("Accept", "application/json");
    
        StringEntity input = new StringEntity("{\"favoriteColor\":\"red\",\"hobby\":\"Kendo\"}");
        input.setContentType(ContentType.APPLICATION_JSON.toString());
        postRequest.setEntity(input);
    
        HttpResponse response = httpClient.execute(postRequest);
        System.out.println(response);