How to get and parse JSON response from x-www-form-urlencoded POST, RestTemplate (Java)?

Mykola :

I have this method to make request:

    @Override
    public HttpEntity<MultiValueMap<String, String>> generateRequestEntity(Date date) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("key", "SOME_API_KEY");
        map.add("source", "SOME_API_SOURCE");
        if (date == null)
            map.add("method", "getall");
        else {
            map.add("method", "getfrom");
            map.add("date", new SimpleDateFormat("yyyy-MM-dd").format(date));
        }

        return new HttpEntity<>(map, headers);
    }

I send a request and get a response, as recommended at following link: URL

HttpEntity<MultiValueMap<String, String>> request = generateRequestEntity(date);
ResponseEntity<OnlineSell[]> response = restTemplate.postForEntity(url, request, OnlineSell[].class);
OnlineSell[] onlineSells = response.getBody();

But I have a problem. I am failing when trying to parse JSON-response. OnlineSell - class, that must keep the answer BUT I just can’t create a structure that successfully stores the values ​​of this answer. A very large and complex answer tree comes.

Answer: Can I get JSONObject from it to parse and save manually? Or can I get help with JSON parsing by previously updating this post and adding it (answer form Postman)?

Léo Schneider :

What you can do is to consider the ResponseEntity as a String. Then afterwards you can use objectMapper with readValue(),

Something like this:

ResponseEntity<String> response = restTemplate().postForEntity(url, request, String.class);

String body = response.getBody();
OnlineSell[] onlineSells = new ObjectMapper().readValue(body, OnlineSell[].class);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to send post request with x-www-form-urlencoded body

How to force Angular2 to POST using x-www-form-urlencoded

Post a x-www-form-urlencoded request from React Native

CORS issue when trying to do JQuery post with json data, but not with plain text or x-www-form-urlencoded

How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel

Add JSON to Parse.Cloud.httpRequest "application/x-www-form-urlencoded"

how to post data in node.js with content type ='application/x-www-form-urlencoded'

How do I post data using okhttp library with content type x-www-form-urlencoded?

How to POST with application/x-www-form-urlencoded header and URLSearchParams using isomorphic-fetch

Swift - How to send POST request with "x-www-form-urlencoded" content-type

How to post (x-www-form-urlencoded) Json Data using Retrofit?

Convert encoded application/x-www-form-urlencoded post data to json object

Convert json to x-www-form-urlencoded

How to POST content as application/x-www-form-urlencoded

Sending post request from axios with "Content-Type" : "application/x-www-form-urlencoded" gives an 401 Unauthorized response

Post controller recognizes only parameters sent by "from-data" or "x-www-form-urlencoded" from postman

How to POST x-www-form-urlencoded in retrofit

How to send post request with content-type x-www-form-urlencoded android retrofit

Akka HTTP how to POST singleRequest with Content-Type application/x-www-form-urlencoded

How to escape + in post call content type as application/x-www-form-urlencoded

How to send post request with x-www-form-urlencoded body with loopj-async-http library

How to get data from webhook with content-type: application/x-www-form-urlencoded;charset=UTF-8?

How to convert x-www-form-urlencoded post Message to JSON post Message?

NodeJS get value from formData x-www-form-urlencoded

How to send x-www-form-urlencoded in a post request in webclient?

how to post body x-www-form-urlencoded using webclient?

How to do a post request using FORM-DATA or x-www-form-urlencoded with Android?

How to convert x-www-form-urlencoded string to json in dart?

How to do a x-www-form-urlencoded POST login using cypress?