简体   繁体   English

如何在 POST 请求中传递 Headers 和 request body?

[英]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 HeaderHttpEntity ,我想将它们传递给 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 件事

  1. HttpEntity实体

  2. HttpHeaders头文件

  3. 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下面的代码在包含HeaderHttpEntity 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 件事

  1. HttpEntity实体
  2. HttpHeaders头文件
  3. request要求

Quoting javadoc of HttpEntity :引用HttpEntity javadoc :

Represents an HTTP request or response entity, consisting of headers and body .表示一个 HTTP请求或响应实体,由headersbody组成。

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.

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