简体   繁体   中英

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. =)

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM