Discuss / Java / postman上请求400是什么原因

postman上请求400是什么原因

Topic source

jasmine

#1 Created at ... [Delete] [Delete and Lock User]

curl命令行请求正常

 curl -v -H "Content-Type: application/json" -d "{\"email\":\"caochengli@aliyun.com\",\"password\":\"password\"}" http://localhost:8080/api/signin

结果

*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /api/signin HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.83.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 55
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Tue, 23 Aug 2022 06:52:15 GMT
<
{"user":{"id":0,"email":"caochengli@aliyun.com","name":"caochengli","createdAt":1661236572194,"createdDateTime":"2022-08-23T14:36:12.194","imageUrl":"https://www.gravatar.com/avatar/62a2bfe40815f5813e25c4e8557a64af"}}* Connection #0 to host localhost left intact

但是用postman请求就400了,是什么原因

postman请求url

http://localhost:8080/api/signin

设置header

Content-Type:application/json

form-data

email: caochengli@aliyun.com
password:password

结果

HTTP Status 400 – Bad Request

根据参考内容判断,问题出在JSON解析时出现了一个HTTP消息不可读的异常。异常原因是解析的JSON中包含了一个无效的UTF-8字符(0xf7),导致解析失败。

根据提供的代码和curl命令,可以确定signIn方法期望接收一个JSON请求体,其中包含email和password字段。根据curl命令,请求中的email字段被设置为"12301@example.com",而password字段被设置为"小明password"。

要解决这个问题,需要确保请求中的JSON是有效的UTF-8编码。可以尝试使用一个有效的UTF-8编码替换掉无效的0xf7字符。比如将请求中的"小明password"替换为一个有效的UTF-8字符串。

修正后的curl命令如下:

复制代码curl -v -H "Content-Type: application/json" -d '{"email":"12301@example.com","password":"\u5c0f\u660epassword"}' http://localhost:8080/api/signin

在这个修正的curl命令中,password字段已经被替换为了"\u5c0f\u660epassword",其中的"\u5c0f\u660e"表示"小明"这个字符串的有效UTF-8编码。

通过执行修正后的curl命令,应该能够成功调用signin方法并获得合适的响应。


  • 1

Reply