【问题标题】:How to run a curl (Using curl command in java) command from java?如何从java运行curl(在java中使用curl命令)命令?
【发布时间】:2021-04-22 23:31:32
【问题描述】:

一旦我在终端中点击 curl 代码,我就能够得到 200,所以我认为到目前为止我编写 testStytch 的方式还可以。但是一旦我尝试集成到一个 java 文件中,我就会得到一个错误的请求响应。我现在一点头绪都没有。 https://github.com/libetl/curl 这就是我所说的转换我的 curl 代码。

这是我得到的错误。 HttpResponseProxy{HTTP/1.1 400 错误请求 [日期:格林威治标准时间 2021 年 4 月 22 日星期四 23:21:42,内容类型:应用程序/json,内容长度:189,连接:保持活动,Traceparent:00-d3a39218eb8d091ffe3bf5cb474692ce-353a1a23da4e5dc4 -01] ResponseEntityProxy{[Content-Type: application/json,Content-Length: 189,Chunked: false]}}

 @RequestMapping(value = "/loginByStytch" , method = RequestMethod.POST)
      public synchronized String loginByStytch(ModelMap model, HttpServletRequest
      request) throws Exception {

      mapper = new ObjectMapper();
      String hashPassword;
      
      UserInfoBean userInfoBean = mapper.readValue(request.getParameter("data"),
      new TypeReference<UserInfoBean>() { });
      
      String testStytch = "-k -X POST 'https://test.stytch.com/v1/magic_links/send_by_email' -u project-test-b3ca64c2-b0c8d73:secret-test-s5lO3O -H 'Content-Type: application/json' -d '{​​​​​​​\"email\":\"yiikikkano@nllllc.com\",\"magic_link_url\":\"http://localhost:8080/ROOT/NC/authenticateByStytch\",\"expiration_minutes\":500}​​​​​​​​'";

      HttpResponse response = curl(testStytch);
      
      InputStream inputStream = response.getEntity().getContent();
      String jsonContent = convert(inputStream, Charset.forName("UTF-8"));

      request.getSession().setAttribute(Constant.SESSION_NAME, null);
      
      return "redirect:/timeline";
      
      }

【问题讨论】:

    标签: java curl libcurl


    【解决方案1】:

    好吧,您可以将 curl 与流程对象一起使用,手工制作的方式,我对您使用的这个库了解不多,但不太为人所知,所以我会避免,您也可以使用它来获取 inputStream,并用于填充响应流:

    String command = "curl -X POST https://your_url/path --data foo=bar"; 
    Process process = Runtime.getRuntime().exec(command);
    process.getInputStream();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-06
      • 2014-07-26
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 2017-08-08
      相关资源
      最近更新 更多