简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM