【问题标题】:Spring Boot MultiValueMap parameters in PostmanPostman 中的 Spring Boot MultiValueMap 参数
【发布时间】:2018-08-14 22:07:16
【问题描述】:

我已经在 Spring Boot 中实现了 OAuth2。在 JUnit 中测试它时效果很好,但是当我在 postman 中尝试 API 时,我总是未经授权。

JUnit 中的测试函数:

private String obtainAccessToken(String username, String password) throws Exception {
    final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.add("grant_type", "password");
    params.add("client_id", CLIENT_ID);
    params.add("username", username);
    params.add("password", password);


    ResultActions result = mockMvc.perform(post("/oauth/token")
            .params(params)
            .with(httpBasic(CLIENT_ID, CLIENT_SECRET))
            .accept(CONTENT_TYPE))
            .andExpect(status().isOk())
            .andExpect(content().contentType(CONTENT_TYPE));


    String resultString = result.andReturn().getResponse().getContentAsString();

    JacksonJsonParser jsonParser = new JacksonJsonParser();
    return jsonParser.parseMap(resultString).get("access_token").toString();
}

我在邮递员中尝试了以下 API:

POST 类型 http://localhost:8080/oauth/token 与内容类型 application/json 在正文部分,我选择了 raw 和 JSON:

{
    "grant_type":"password",
    "client_id":"clientIdPassword",
    "username":"a",
    "password":"a"
}

显示 401 Unauthorized。然后我也这样尝试:

内容类型为application/jsonhttp://localhost:8080/oauth/token?grant_type=password&amp;client_id=clientIdPassword&amp;username=a&amp;password=a 的帖子类型。这也显示 401 Unauthorized。

我的问题是如何在 Postman 中将 MultiValueMap 设置为参数?

【问题讨论】:

  • 从 Postman 中设置基本身份验证?
  • 嘿@Eniss 你得到解决方案了吗?

标签: java spring spring-boot junit postman


【解决方案1】:

您应该使用邮递员的授权选项卡来发送身份验证标头以及您喜欢的请求正文。 PFA样本图片

【讨论】:

    【解决方案2】:

    当您通过 POSTMAN 工具发送请求时,选择 HTTP 方法的类型(POST、PUT、DELETE),然后在“Body”选项卡中选择“raw”选项,然后在其中添加 Map 的 JSON。确保您在 "Headers" 中选择了 "Content-type" 作为 "application/json"。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      • 2019-08-22
      • 2021-07-19
      相关资源
      最近更新 更多