4

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.

user7699179
  • 81
  • 2
  • 6
  • Can you try this `URIBuilder exampleUri = new URIBuilder(baseUri+"/"+userId);` – Sachin Mesare Mar 08 '18 at 06:30
  • I am getting the expected url after doing the following URIBuilder exampleUri = new URIBuilder(baseUri+"/"+userId); https://www.example.com/25efba57-673b-45ae-9eed-41588730aaaa However I am getting unauthorized 401 because the userId is not getting added as path parameter. – user7699179 Mar 08 '18 at 06:34

2 Answers2

3
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

Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
  • I am getting the following error HttpResponseProxy{HTTP/1.1 405 HTTP method POST is not supported by this URL However I am able to do a post from swagger. – user7699179 Mar 08 '18 at 06:50
1

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

AmitKhiwal
  • 59
  • 6
  • Tried with "/" and without slash but doesnt give me the response it only says post is not support for this url. However same works from mandrake. – user7699179 Mar 08 '18 at 07:04