[英]How to pass Headers and request body in POST request?
I am using RestTemplate restTemplate.exchange
method to POST request to an endpoint.我正在使用 RestTemplate restTemplate.exchange
方法将请求发送到端点。 I have OAuth Header
and HttpEntity
in different file which I want to pass to POST request, in addition to this I also want to pass request
to the endpoint.我在不同的文件中有OAuth Header
和HttpEntity
,我想将它们传递给 POST 请求,除此之外,我还想将request
传递给端点。
I was able to successfully pass the headers and request, but not Http entity which contains credentials我能够成功传递标头和请求,但不能成功传递包含凭据的 Http 实体
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST,
new HttpEntity<>(request, dataRepo.getHeader()), String.class);
Is there any way I can pass all 3 things有什么办法可以通过所有 3 件事
HttpEntity实体
HttpHeaders头文件
request要求
Here is my code这是我的代码
@RunWith(MockitoJUnitRunner.class)
@TestPropertySource
public class DataTest {
@Inject
private Oauth oauth;
@Mock
private DataRepo dataRepo;
RestTemplate restTemplate = new RestTemplate();
@Qualifier(OAuth2HttpHeadersBuilder.BEAN_NAME)
NewHttpHeader headersBuilder;
@Test
public void testAddEmployeeSuccess() throws URISyntaxException {
URI uri = new URI(url);
Set<String> mockData = Stream.of("A","B").collect(Collectors.toSet());
String onsString = String.join(",", mockData);
Map<String, String> requestBody = new HashMap<>();
requestBody.put("name", onsString);
JSONObject jsonObject = new JSONObject(requestBody);
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), null);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST,
new HttpEntity<>(request, dataRepo.getHeader()), String.class);
Assert.assertEquals(201, result.getStatusCodeValue());
}
The below code is in NewHttpHeader.java file which contains Header
and HttpEntity
下面的代码在包含Header
和HttpEntity
NewHttpHeader.java 文件中
private HttpEntity<MultiValueMap<String,String>> getHttpEntity() {
MultiValueMap<String, String> store = new LinkedMultiValueMap<>();
store.add( "pas", "password" );
store.add( "name", config.getVaultServiceAccountName() );
return new HttpEntity<>( store, getHeader() );
}
private HttpHeaders getHeader() {
HttpHeaders httpHeaders = headersBuilder.build();
httpHeaders.add( HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType() );
httpHeaders.add( HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType() );
return httpHeaders;
}
}
Quoting question:引用问题:
Is there any way I can pass all 3 things有什么办法可以通过所有 3 件事
- HttpEntity实体
- HttpHeaders头文件
- request要求
Quoting javadoc of HttpEntity
:引用HttpEntity
javadoc :
Represents an HTTP request or response entity, consisting of headers and body .表示一个 HTTP请求或响应实体,由headers和body组成。
So the answer to your question is: Yes , you can pass all 3, since the first is nothing but a combination of the other two.所以你的问题的答案是:是的,你可以通过所有 3,因为第一个只是其他两个的组合。
Just merge your two HttpEntity
objects.只需合并您的两个HttpEntity
对象。
Before前
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), null);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST,
new HttpEntity<>(request, dataRepo.getHeader()), String.class);
After后
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), dataRepo.getHeader());
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST,
request, String.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.