如何从java运行curl(在java中使用curl命令)命令?

zxlwwiss  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(435)

一旦我在终端中输入curl代码,我就可以得到200,所以我假设我编写teststytch的方式到目前为止还可以。但是,一旦我试图集成到一个java文件中,就会得到一个错误的请求响应。我现在有点不知所措。https://github.com/libetl/curl 这就是我所说的转换我的curl代码。
这就是我犯的错误。httpresponseproxy{http/1.1 400坏请求[date:thu,22 apr 2021 23:21:42 gmt,content type:application/json,content length:189,connection:keep alive,traceparent:00-d3a39218eb8d091ffe3bf5cb4746922ce-353a1a23da4e5dc4-01]响应代理{[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";

      }
5w9g7ksd

5w9g7ksd1#

好吧,你可以用curl和一个进程对象,手工制作的方式,我不太了解你用的这个lib,但是不太了解,所以我会避免,你也可以用这个得到一个inputstream,用来填充响应流:

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

相关问题