简体   繁体   English

我如何在春季从curl -X请求获取参数?

[英]How can i get params in spring from curl -X request?

I have a problem with processing params from curl request to my spring-boot app. 我在处理从curl请求到spring-boot应用程序的参数时遇到问题。

My controller post method: 我的控制器发布方法:

@RequestMapping(value = "/cat", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity addCat(@Valid @ModelAttribute NewCatRequest newCatRequest, BindingResult bindingResult) {
    System.out.println( newCatRequest);
    if (bindingResult.hasErrors()) {
        return ResponseEntity.badRequest().body(bindingResult.getFieldError().getDefaultMessage());
    }

    int rowsAffected = catsRepository.saveCat(newCatRequest.getName(), newCatRequest.getColor(), newCatRequest.getTail_length(), newCatRequest.getWhiskers_length());
    if (rowsAffected == 1) {
        return ResponseEntity.ok().body(newCatRequest);
    } else {
        return ResponseEntity.badRequest().body("There was an unexpected error while trying to create cat for you :(");
    }
}

And the problem is: when i'm trying to send this with curl: 问题是:当我尝试使用curl发送此文件时:

curl -X POST http://localhost:8080/cat \\ -d "{\\"name\\": \\"Tihon\\", \\"color\\": \\"red & white\\", \\"tail_length\\": 15, \\"whiskers_length\\": 12}" curl -X POST http:// localhost:8080 / cat \\ -d“ {\\” name \\“:\\” Tihon \\“,\\” color \\“:\\” red&white \\“,\\” tail_length \\“: 15,\\“ whiskers_length \\”:12}“

I have all null params in 'newCatRequest': NewCatRequest{name='null', color='null', tail_length=0, whiskers_length=0} 我在'newCatRequest'中具有所有空参数:NewCatRequest {name ='null',color ='null',tail_length = 0,whiskers_length = 0}

BUT when i'm trying to do the same with Postman (POST method, x-www-form-urlencoded in body with my params) i have a valid result: Result from Postman 但是,当我尝试对Postman执行相同操作时(POST方法,带有我的参数的x-www-form-urlencode在体内)我有一个有效的结果: Postman的结果

Help me pls to understand what's the problem. 请帮助我了解问题所在。

you can use the code option below send button in postman to generate the exact curl request which will work for you https://i.stack.imgur.com/hbk8M.png in code drop down menu search for curl and it will generate a neat and clean curl request for you . 您可以使用邮递员中发送按钮下方的代码选项来生成确切的卷曲请求,该请求将对您有效https://i.stack.imgur.com/hbk8M.png在代码下拉菜单中搜索卷曲,它将生成一个干净利落的卷曲要求。 Hope this helps 希望这可以帮助

Try this: 尝试这个:

curl -X POST \
  http://localhost:8080/cat \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'name=Thidon&color=red%20%26%20white&tail_length=15&whiskers_length=12'

You forgot the header application/x-www-form-urlencoded and the body should not be in json format. 您忘记了标头application/x-www-form-urlencoded ,并且正文不应采用json格式。

curl -X POST http://localhost:8080/cat \
     -d "{\"name\": \"Tihon\", \"color\": \"red & white\", \"tail_length\": 15, \"whiskers_length\": 12}"

The above curl request has a JSON body, whereas your request processing method 上面的curl请求具有JSON主体,而您的请求处理方法

@RequestMapping(value = "/cat", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)

consumes/accepts: application/x-www-form-urlencoded . 消费/接受: application/x-www-form-urlencoded Therefore, you should either convert your method to consume/accept application/json or change your curl request to: 因此,您应该将方法转换为使用/接受application/json或将curl请求更改为:

curl -X POST http://localhost:8080/cat \
     -d 'name=Tihon&color=red%20%26%20white&tail_length=15&whiskers_length= 12' \
     -H 'Content-Type: application/x-www-form-urlencoded'

EDIT 1 编辑1

Please note that the default Content-Type for curl is application/x-www-form-urlencoded . 请注意, curl的默认Content-Typeapplication/x-www-form-urlencoded To use JSON, change your request to: 要使用JSON,请将您的请求更改为:

curl -X POST http://localhost:8080/cat \
     -d "{\"name\": \"Tihon\", \"color\": \"red & white\", \"tail_length\": 15, \"whiskers_length\": 12}" \
     -H 'Content-Type: application/json'

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

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