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

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

一旦我在终端中输入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]}

  1. @RequestMapping(value = "/loginByStytch" , method = RequestMethod.POST)
  2. public synchronized String loginByStytch(ModelMap model, HttpServletRequest
  3. request) throws Exception {
  4. mapper = new ObjectMapper();
  5. String hashPassword;
  6. UserInfoBean userInfoBean = mapper.readValue(request.getParameter("data"),
  7. new TypeReference<UserInfoBean>() { });
  8. 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}​​​​​​​​'";
  9. HttpResponse response = curl(testStytch);
  10. InputStream inputStream = response.getEntity().getContent();
  11. String jsonContent = convert(inputStream, Charset.forName("UTF-8"));
  12. request.getSession().setAttribute(Constant.SESSION_NAME, null);
  13. return "redirect:/timeline";
  14. }
5w9g7ksd

5w9g7ksd1#

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

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

相关问题