Setting request header content-type to json in Spring Framework resttemplate

user2752012

I'm learning Spring Framework to create a client of a REST web service that uses basic authentication and exchanges JSON. After much searching on the web, I wrote some code that worked (below), but now I'm getting an "Unsupported Media Type" error because the requests are sent with Content-Type text/plain rather than application/json. I've found nothing on the web that shows how to set Content-Type in the request header (without getting completely lost in the weeds). My code is:

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

...

BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("login", "password"));
HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

RestTemplate restTemplate = new RestTemplate(requestFactory);
String url = "http://host:8080/path/";
String postBody = getPostInput("filename");
jsonString = restTemplate.postForObject(path, postBody, String.class);

Any guidance would be greatly appreciated.

Thanks, George

ManojP

you can try using any method from below code

1

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> entity = new HttpEntity<String>(postBodyJson ,headers);
restTemplate.put(uRL, entity);

2

RequestEntity<String> requestEntity = RequestEntity .post(new URL(attributeLookupUrl).toURI()) .contentType(MediaType.APPLICATION_JSON) .body(postBodyJson); 
restTemplate.exchange(requestEntity, responseClass);

3

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

// if you need to pass form parameters in request with headers.
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", userName);
map.add("password", password);

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<TokenVO> responses = restTemplate.postForEntity(URL, request, responseClass);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Set default content type header of Spring RestTemplate

From Dev

Setting response content type header in JSONP request?

From Dev

Setting response content type header in JSONP request?

From Dev

Setting custom header on Spring RestTemplate GET call

From Java

How to set an "Accept:" header on Spring RestTemplate request?

From Dev

Set content-type header to json for request.post

From Dev

RestTemplate receive html content type and not json

From Dev

RestTemplate receive html content type and not json

From Dev

Spray not setting Content-Type header for FormData

From Dev

JAX-RS: Consuming JSON POST data without setting Content-Type header to application/json

From Dev

Spring @RestController Get Request Content-Type to response json or html

From Dev

Spring @RestController Get Request Content-Type to response json or html

From Dev

How to get Response Header information from Spring RestTemplate GET request

From Dev

Content-Type - WebAPI - Request Header

From Dev

How to send request Header is "Content-Type":"application/json" when GET on Volley

From Dev

Content type 'null' not supported returned by Spring RESTTemplate getForObject method

From Dev

Content type 'null' not supported returned by Spring RESTTemplate getForObject method

From Dev

Websocket protocol request header and response header no content type

From Dev

Setting default Content Output Type Header in Axis2

From Dev

Setting the 'charset' property on the Content-Type header in the golang HTTP FileServer

From Dev

Setting default Content Output Type Header in Axis2

From Java

HTTP Content-Type Header and JSON

From Dev

The usage of `header("Content-type:application/json");`

From Dev

Setting header in the Http Request

From Dev

Set content length header in java play framework http request

From Dev

Setting JSON request header in Angular2 HTTP POST

From Dev

Post request to include 'Content-Type' and JSON

From Dev

WebApi - The request contains an entity body but no Content-Type header

From Java

How do you set the Content-Type header for an HttpClient request?

Related Related

  1. 1

    Set default content type header of Spring RestTemplate

  2. 2

    Setting response content type header in JSONP request?

  3. 3

    Setting response content type header in JSONP request?

  4. 4

    Setting custom header on Spring RestTemplate GET call

  5. 5

    How to set an "Accept:" header on Spring RestTemplate request?

  6. 6

    Set content-type header to json for request.post

  7. 7

    RestTemplate receive html content type and not json

  8. 8

    RestTemplate receive html content type and not json

  9. 9

    Spray not setting Content-Type header for FormData

  10. 10

    JAX-RS: Consuming JSON POST data without setting Content-Type header to application/json

  11. 11

    Spring @RestController Get Request Content-Type to response json or html

  12. 12

    Spring @RestController Get Request Content-Type to response json or html

  13. 13

    How to get Response Header information from Spring RestTemplate GET request

  14. 14

    Content-Type - WebAPI - Request Header

  15. 15

    How to send request Header is "Content-Type":"application/json" when GET on Volley

  16. 16

    Content type 'null' not supported returned by Spring RESTTemplate getForObject method

  17. 17

    Content type 'null' not supported returned by Spring RESTTemplate getForObject method

  18. 18

    Websocket protocol request header and response header no content type

  19. 19

    Setting default Content Output Type Header in Axis2

  20. 20

    Setting the 'charset' property on the Content-Type header in the golang HTTP FileServer

  21. 21

    Setting default Content Output Type Header in Axis2

  22. 22

    HTTP Content-Type Header and JSON

  23. 23

    The usage of `header("Content-type:application/json");`

  24. 24

    Setting header in the Http Request

  25. 25

    Set content length header in java play framework http request

  26. 26

    Setting JSON request header in Angular2 HTTP POST

  27. 27

    Post request to include 'Content-Type' and JSON

  28. 28

    WebApi - The request contains an entity body but no Content-Type header

  29. 29

    How do you set the Content-Type header for an HttpClient request?

HotTag

Archive