【问题标题】:Stormpath Rest api JavaStormpath Rest api Java
【发布时间】:2015-10-25 23:35:59
【问题描述】:

谁能帮我用java中的httpRequest的形式写这个。我试了很多次都失败了。我不知道为什么,但我就是做错了=(

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"

(自定义数据必须是json格式)

任何帮助将不胜感激。 =)

【问题讨论】:

    标签: java rest httprequest stormpath


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2017-05-04
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多