Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, September 5, 2016

How to convert map to url query string?

How to convert map to url query string?


Do you know of any utility class/library, that can convert Map into URL-friendly query string?

Example:
I have a map:
- "param1"=12,
- "param2"="cat"

I want to get: param1=12?m2=cat.

PS. I know I can easily write it myself, I am just surprised that I cannot find it anywhere (I checked Apache Commons so far).

Answer by polygenelubricants for How to convert map to url query string?


Here's something that I quickly wrote; I'm sure it can be improved upon.

import java.util.*;  import java.io.UnsupportedEncodingException;  import java.net.URLEncoder;    public class MapQuery {      static String urlEncodeUTF8(String s) {          try {              return URLEncoder.encode(s, "UTF-8");          } catch (UnsupportedEncodingException e) {              throw new UnsupportedOperationException(e);          }      }      static String urlEncodeUTF8(Map map) {          StringBuilder sb = new StringBuilder();          for (Map.Entry entry : map.entrySet()) {              if (sb.length() > 0) {                  sb.append("&");              }              sb.append(String.format("%s=%s",                  urlEncodeUTF8(entry.getKey().toString()),                  urlEncodeUTF8(entry.getValue().toString())              ));          }          return sb.toString();             }      public static void main(String[] args) {          Map map = new HashMap();          map.put("p1", 12);          map.put("p2", "cat");          map.put("p3", "a & b");                   System.out.println(urlEncodeUTF8(map));          // prints "p3=a+%26+b&p2=cat&p1=12"      }  }  

Answer by ZZ Coder for How to convert map to url query string?


The most robust one I saw off-shelf is the URLEncodedUtils class from Apache Http Compoments (HttpClient 4.0).

The method URLEncodeUtils.format() is what you need.

It doesn't use map so you can have duplicate parameter names, like,

  a=1&a=2&b=3  

Not that I recommend this kind of use of parameter names.

Answer by Thraidh for How to convert map to url query string?


If you actually want to build a complete URI, try URIBuilder from Apache Http Compoments (HttpClient 4).

This does not actually answer the question, but it answered the one I had when I found this question.

Answer by Christophe Roussy for How to convert map to url query string?


Another 'one class'/no dependency way of doing it:

import java.io.UnsupportedEncodingException;  import java.net.URLEncoder;  import java.util.ArrayList;  import java.util.Arrays;  import java.util.Iterator;  import java.util.LinkedHashMap;  import java.util.List;  import java.util.Map;  import java.util.Map.Entry;    public class UrlQueryString {    private static final String DEFAULT_ENCODING = "UTF-8";      public static String buildQueryString(final LinkedHashMap map) {      try {        final Iterator> it = map.entrySet().iterator();        final StringBuilder sb = new StringBuilder(map.size() * 8);        while (it.hasNext()) {          final Map.Entry entry = it.next();          final String key = entry.getKey();          if (key != null) {            sb.append(URLEncoder.encode(key, DEFAULT_ENCODING));            sb.append("=");            final Object value = entry.getValue();            final String valueAsString = value != null ? URLEncoder.encode(value.toString(), DEFAULT_ENCODING) : "";            sb.append(valueAsString);            if (it.hasNext()) {              sb.append("&");            }          } else {            // Do what you want...for example:            assert false : String.format("Null key in query map: %s", map.entrySet());          }        }        return sb.toString();      } catch (final UnsupportedEncodingException e) {        throw new UnsupportedOperationException(e);      }    }      public static String buildQueryStringMulti(final LinkedHashMap> map) {      try {        final StringBuilder sb = new StringBuilder(map.size() * 8);        for (final Iterator>> mapIterator = map.entrySet().iterator(); mapIterator.hasNext();) {          final Entry> entry = mapIterator.next();          final String key = entry.getKey();          if (key != null) {            final String keyEncoded = URLEncoder.encode(key, DEFAULT_ENCODING);            final List values = entry.getValue();            sb.append(keyEncoded);            sb.append("=");            if (values != null) {              for (final Iterator listIt = values.iterator(); listIt.hasNext();) {                final Object valueObject = listIt.next();                sb.append(valueObject != null ? URLEncoder.encode(valueObject.toString(), DEFAULT_ENCODING) : "");                if (listIt.hasNext()) {                  sb.append("&");                  sb.append(keyEncoded);                  sb.append("=");                }              }            }            if (mapIterator.hasNext()) {              sb.append("&");            }          } else {            // Do what you want...for example:            assert false : String.format("Null key in query map: %s", map.entrySet());          }        }        return sb.toString();      } catch (final UnsupportedEncodingException e) {        throw new UnsupportedOperationException(e);      }    }      public static void main(final String[] args) {      {        final LinkedHashMap queryItems = new LinkedHashMap();        queryItems.put("brand", "C&A");        queryItems.put("count", null);        queryItems.put("misc", 42);        final String buildQueryString = buildQueryString(queryItems);        System.out.println(buildQueryString);      }      {        final LinkedHashMap> queryItems = new LinkedHashMap>();        queryItems.put("usernames", new ArrayList(Arrays.asList(new String[] { "bob", "john" })));        queryItems.put("nullValue", null);        queryItems.put("misc", new ArrayList(Arrays.asList(new Integer[] { 1, 2, 3 })));        final String buildQueryString = buildQueryStringMulti(queryItems);        System.out.println(buildQueryString);      }    }  }      

You may use either simple (easier to write in most cases) or multiple when required. Note that both can be combined by adding an ampersand... If you find any problems let me know in the comments.

Answer by Senthil Arumugam SP for How to convert map to url query string?


In Spring Util, there is a better way..,

import org.springframework.util.LinkedMultiValueMap;  import org.springframework.util.MultiValueMap;  import org.springframework.util.concurrent.ListenableFuture;  import org.springframework.web.util.UriComponents;  import org.springframework.web.util.UriComponentsBuilder;    MultiValueMap params = new LinkedMultiValueMap();  params.add("key", key);  params.add("storeId", storeId);  params.add("orderId", orderId);  UriComponents uriComponents =     UriComponentsBuilder.fromHttpUrl("http://spsenthil.com/order").queryParams(params).build();  ListenableFuture> responseFuture =     restTemplate.getForEntity(uriComponents.toUriString(), String.class);  

Answer by eclipse for How to convert map to url query string?


I found a smooth solution using java 8 and polygenelubricants' solution.

parameters.entrySet().stream()      .map(p -> urlEncodeUTF8(p.getKey()) + "=" + urlEncodeUTF8(p.getValue()))      .reduce((p1, p2) -> p1 + "&" + p2)      .orElse("");  

Answer by Marlon Bernardes for How to convert map to url query string?


This is the solution I implemented, using Java 8 and org.apache.http.client.URLEncodedUtils. It maps the entries of the map into a list of BasicNameValuePair and then uses Apache's URLEncodedUtils to turn that into a query string.

List nameValuePairs = params.entrySet().stream()     .map(entry -> new BasicNameValuePair(entry.getKey(), entry.getValue()))     .collect(Collectors.toList());    URLEncodedUtils.format(nameValuePairs, Charset.forName("UTF-8"));  

Answer by Auke for How to convert map to url query string?


To improve a little bit upon @eclipse's answer: In Javaland a request parameter map is usually represented as a Map, a Map> or possibly some kind of MultiValueMap which is sort of the same thing. In any case: a parameter can usually have multiple values. A Java 8 solution would therefore be something along these lines:

public String getQueryString(HttpServletRequest request, String encoding) {      Map parameters = request.getParameterMap();        return parameters.entrySet().stream()              .flatMap(entry -> encodeMultiParameter(entry.getKey(), entry.getValue(), encoding))              .reduce((param1, param2) -> param1 + "&" + param2)              .orElse("");  }    private Stream encodeMultiParameter(String key, String[] values, String encoding) {      return Stream.of(values).map(value -> encodeSingleParameter(key, value, encoding));  }    private String encodeSingleParameter(String key, String value, String encoding) {      return urlEncode(key, encoding) + "=" + urlEncode(value, encoding);  }    private String urlEncode(String value, String encoding) {      try {          return URLEncoder.encode(value, encoding);      } catch (UnsupportedEncodingException e) {          throw new IllegalArgumentException("Cannot url encode " + value, e);      }  }  

Answer by arcseldon for How to convert map to url query string?


Update June 2016

Felt compelled to add an answer having seen far too many SOF answers with dated or inadequate answers to very common problem - a good library and some solid example usage for both parse and format operations.

Use org.apache.httpcomponents.httpclient library. The library contains this org.apache.http.client.utils.URLEncodedUtils class utility.

For example, it is easy to download this dependency from Maven:

       org.apache.httpcomponents      httpclient      4.5     

For my purposes I only needed to parse (read from query string to name-value pairs) and format (read from name-value pairs to query string) query strings. However, there are equivalents for doing the same with a URI (see commented out line below).

// Required imports

import org.apache.http.NameValuePair;  import org.apache.http.client.utils.URLEncodedUtils;  import java.io.UnsupportedEncodingException;  import java.net.URLDecoder;  import java.nio.charset.StandardCharsets;  

// code snippet

public static void parseAndFormatExample() throws UnsupportedEncodingException {          final String queryString = "nonce=12345&redirectCallbackUrl=http://www.bbc.co.uk";          System.out.println(queryString);          // => nonce=12345&redirectCallbackUrl=http://www.bbc.co.uk            final List params =                  URLEncodedUtils.parse(queryString, StandardCharsets.UTF_8);          // List params = URLEncodedUtils.parse(new URI(url), "UTF-8");            for (final NameValuePair param : params) {              System.out.println(param.getName() + " : " + param.getValue());              // => nonce : 12345              // => redirectCallbackUrl : http://www.bbc.co.uk          }            final String newQueryStringEncoded =                  URLEncodedUtils.format(params, StandardCharsets.UTF_8);              // decode when printing to screen          final String newQueryStringDecoded =                  URLDecoder.decode(newQueryStringEncoded, StandardCharsets.UTF_8.toString());          System.out.println(newQueryStringDecoded);          // => nonce=12345&redirectCallbackUrl=http://www.bbc.co.uk      }  

This library did exactly what I needed and was able to replace some hacked custom code.

Answer by Jishin Dev for How to convert map to url query string?


Using retrofit 2.0 can help you simplify and organise your endpoint calls using simple annotations.

For converting a Map to a Query, retrofit provides a parameter annotation, which can be used as

@GET("your_end_point") Call getResults(@QueryMap Map queryMap);

This automatically converts the map to a url query string.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.