CURL中-u的含义是什么,以及如何使用带有-u的restTemplate发布请求

时间:2017-09-14 07:53:37

标签: java spring authentication curl resttemplate

我有cURL

curl -i https://thisIsValidUrl \
 -X PUT \
 -H "Content-Type: application/json" \
 -u YOUR-SITE-ID-HERE:YOUR-SECRET-API-KEY-HERE \
 -d '{"email":"customer@example.com","created_at":1361205308,"first_name":"Bob","plan":"basic"}'

我需要使用spring restTemplate发布请求,但我找不到如何使用-u

1 个答案:

答案 0 :(得分:1)

发现它,它只是意味着使用基本身份验证,我应该将YOUR-SITE-ID-HERE:YOUR-SECRET-API-KEY-HERE加密为Base64字符串并在Authorization标题中使用它

    MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
    RestTemplate restTemplate = new RestTemplate();

    headers.add("Authorization", "Basic " + Base64String);
    headers.add("Content-Type", "application/json");
    .
    .
    .

    HttpEntity<RaisEventRequest> request = new HttpEntity<RaisEventRequest>(RaisEventRequest, headers);

    ResponseEntity<RaisEventResponse> responseEntity = restTemplate
            .exchange(eventsURL, HttpMethod.POST, request, RaisEventResponse.class);

    return responseEntity.getBody();