【问题标题】:How to add path parameters in httpclient when building rest-api构建rest-api时如何在httpclient中添加路径参数
【发布时间】:2018-03-08 06:23:25
【问题描述】:

我有以下 uri

www.xyz.com - 基础 uri 我需要将 userId 作为路径参数添加到 uri 的末尾。

根据 swagger 文档,uri 格式为 www.example.com/{userId}

参数类型 = Path ,Value = userId(String)

我可以在帖子请求中添加正文。但是我无法将 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 post中添加路径参数。我也尝试了 setparameter 方法,但仍然无法添加它。

【问题讨论】:

  • 你可以试试这个URIBuilder exampleUri = new URIBuilder(baseUri+"/"+userId);
  • 在执行以下 URIBuilder exampleUri = new URIBuilder(baseUri+"/"+userId); example.com/25efba57-673b-45ae-9eed-41588730aaaa 但是我得到了未经授权的 401,因为 userId 没有被添加为路径参数。

标签: java apache httpclient jira-rest-java-api


【解决方案1】:
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

【讨论】:

  • 我收到以下错误 HttpResponseProxy{HTTP/1.1 405 HTTP 方法 POST 不受此 URL 支持但是我可以从 swagger 发帖。
【解决方案2】:

尝试连接字符串 baseUri 和 userId。 还要检查 baseUri 是否有斜杠“/”或在连接之前添加它。

【讨论】:

  • 尝试使用“/”且不使用斜杠,但没有给我回复,它只说帖子不支持此网址。然而,同样适用于曼德拉。
猜你喜欢
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
相关资源
最近更新 更多