Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add path parameters in httpclient when building rest-api

I have the following uri

www.xyz.com - base uri I need to add userId to end of the uri as path parameter.

As per swagger document the uri format is www.example.com/{userId}

Parameter type = Path ,Value = userId(String)

I could add body to the post request. However I am not able to add userId as path parameter. I was only able to add the parameter is query parameter but have difficulty in adding it as path parameter.

Code Snippet

CloseableHttpClient client = HttpClients.createDefault();
String userId = "25efba57-673b-45ae-9eed-41588730aaaa";
String baseUri = "http://www.example.com";
URIBuilder exampleUri = new URIBuilder(baseUri);
exampleUri.addParameter("userId",userId);
String generatedUri = mandrakeUri.toString();
HttpPost httpPost = new HttpPost(generatedUri);

Generated URI = https://www.example.com/?userId=25efba57-673b-45ae-9eed-41588730aaaa

Expected URI = https://www.example.com/25efba57-673b-45ae-9eed-41588730aaaa

Please help me to add path parameter in http post. I also tried setparameter method but still not able to add it.

like image 806
user7699179 Avatar asked Sep 12 '25 03:09

user7699179


2 Answers

String baseUri = "http://www.example.com";    
URIBuilder exampleUri = new URIBuilder(baseUri);
exampleUri.setPath("/" + userId);
String generatedUri = mandrakeUri.toString();
HttpPost httpPost = new HttpPost(generatedUri)

Refer the docs here

like image 155
Sagar Pudi Avatar answered Sep 13 '25 16:09

Sagar Pudi


Try concatenating the Strings baseUri and userId. Also check if baseUri has a slash "/" or add it before concatenating.

like image 30
AmitKhiwal Avatar answered Sep 13 '25 18:09

AmitKhiwal