簡體   English   中英

構建rest-api時如何在httpclient中添加路徑參數

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

我有以下uri

www.xyz.com-基本uri我需要在uri的末尾添加userId作為路徑參數。

根據一般文件,uri格式為www.example.com/{userId}

參數類型=路徑,值= userId(字符串)

我可以在發布請求中添加正文。 但是,我無法將userId添加為路徑參數。 我只能將參數添加為查詢參數,但是很難將其添加為路徑參數。

代碼段

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);

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

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

請幫助我在http帖子中添加path參數。 我也嘗試了setparameter方法,但仍然無法添加它。

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

在這里參考文檔

嘗試串聯字符串baseUri和userId。 還要檢查baseUri是否帶有斜杠“ /”,或者在連接之前添加它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM