简体   繁体   English

通过curl命令发送POST请求

[英]Sending POST request by curl command

I have problem with sending POST request by curl command. 我通过curl命令发送POST请求时遇到问题。

     @RequestMapping(value = "/abc/def/{parameter}/enum", method = RequestMethod.POST)
     public ResponseEntity<classA> function(@PathVariable(value = "parameter") int parameter, @RequestBody String parameter2) {
           a = list.get(parameter);
           a.setParameter(enumA.getValue(parameter2));
           ResponseEntity<classA> response = new ResponseEntity<>(a, HttpStatus.OK);
          return response;
     }

Then i want to send POST by curl command: 然后我想通过curl命令发送POST:

curl -H "Content-Type: application/json" -X POST -d '{"parameter2" : "enum"}' https://user:password@localhost:port/abc/def/1/enum -k

I get response: 我得到回应:

{"timestamp":123456789,"status":403,"error":"Forbidden","message":"Expected CSRF token not found. Has your session expired?","path":"/abc/def/1/enum/"}

Ideas? 有想法吗?

The problem is: 问题是:

Expected CSRF token not found.

Your aplication (Spring MVC as i can see) have CSRF protection enabled, so you need to send the "_csrf" param with the post. 您的应用程序(如我所见,是Spring MVC)已启用CSRF保护,因此您需要在发布时发送“ _csrf”参数。 More info at: 有关更多信息,请访问:
http://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html http://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html
https://spring.io/blog/2013/08/21/spring-security-3-2-0-rc1-highlights-csrf-protection/ https://spring.io/blog/2013/08/21/spring-security-3-2-0-rc1-highlights-csrf-protection/

The CSRF token value changes with the user session, if you want to see this csrf token you can visit your aplication with the web browser and see the HTML code of your page, in the form tag you will see something like this: CSRF令牌值随用户会话而变化,如果要查看此csrf令牌,则可以使用Web浏览器访问应用程序并查看页面的HTML代码,在form标记中,您将看到类似以下内容:

<input type="hidden"
    name= _csrf
    value= 964f8675-a57a-4f85-b196-976d71ffef96 />

So you need to send this param within your POST. 因此,您需要在POST中发送此参数。

curl -H "Content-Type: application/json" -X POST -d '{"parameter2" : "enum","_csrf":"964f8675-a57a-4f85-b196-976d71ffef96"}' -u username:password https://localhost:port/abc/def/1/enum

CARE!: as I said, this token will change with the user session, so you will not be able to use the same token always. 小心!:正如我所说,此令牌将随用户会话而变化,因此您将无法始终使用同一令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM