HTTP GET Method – Ways to form GET URL

In this article let us see the ways to form GET URL for http requests. We are going to use Spring Boot and Google Chrome Postman to verify the URL formation.

Common ways of forming GET url’s are by

  1. Path
  2. Body

Query: (Path)

When GET URL is formed along with query, the URL will look like,

http://example/{id}

Params: (Body)

When GET URL is formed along with Params, we will send the parameters along with the body.

http://example?id=1

There are four parameters with request param,

  • defaultValue – This is the default value, if the request is not having the value or it is empty.
  • name – Parameter name
  • required – true, if we want to make the parameter required one.
  • value – value of the parameter

Now let us see an example – Various ways to form GET URL.
I have created a simple springboot application with a controller. The output shown below are from chrome postman application.

you can download the code here

package com.javainfinite;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.client.utils.URIBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;


@RestController
public class UrlController {
	
	@GetMapping("/javainfinite")
	public String homepage() {
		return "success";
	}
	
	@GetMapping("/javainfinite/{id}")
	public String pathVariableId(@PathVariable("id") Integer id) {
		return "Welcome to Java Infinite - id: "+id;
		
	}
	
	@GetMapping("/javainfinite/idParam")
	public String idParam(@RequestParam("id") Integer id) {
		return "Welcome to Java Infinite - Param ID: "+id;
	}
	
	@GetMapping("/javainfinite/params")
	public String multipleParams(@RequestParam("name") String name, @RequestParam("age") String age ) {
		return "Welcome to Java Infinite - Multiple Params - Name: "+name+" age: "+age;
	}
	
	@GetMapping("/javainfinite/uriComponentBuild")
	public String uriComponentBuild() {
		UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://javainfinite.com");
		builder.queryParam("id", "1");
		builder.queryParam("One", "2");
		String url = builder.toUriString();
		return url;
	}
	
	@GetMapping("/javainfinite/uriBuild")
	public String formation() {
		String uriBuild = null;
		try {
			URIBuilder b = new URIBuilder("https://javainfinite.com");
			b.addParameter("one", "1");
			b.addParameter("two", "two");
			URL url = b.build().toURL();
			uriBuild = url.toString();
			
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return uriBuild;
	}

}
package com.javainfinite;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;


/**
 * java infinite!
 *
 */
@SpringBootApplication
@ComponentScan("com.*")
public class App 
{
    public static void main( String[] args )
    {
    	SpringApplication.run(App.class, args);
    }
}

Output:
/javainfinite

 

/javainfinite/{id}


/javainfinite/idParam

/javainfinite/params

/javainfinite/uriComponentBuild

/javainfinite/uriBuild

 

Download here

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *