# HTTP错误码和出现场景

> 状态代码为3位数字。 1xx：指示信息--表示请求已接收，继续处理。 2xx：成功--表示请求已被成功接收、理解、接受。 3xx：重定向--要完成请求必须进行更进一步的操作。 4xx：客户端错误--请求有语法错误或请求无法实现。 5xx：服务器端错误--服务器未能实现合法的请求。

## 400错误

```
"error":"Bad Request",
"message":"Required String parameter 'name' is not present",
```

**场景：**

由于语法格式有误，服务器无法理解此请求。比如上面的错误就是我使用springboot发布了一个服务：

```
@RequestMapping(value = "/adduser", method = RequestMethod.POST)
@ResponseBody
public User addUser(@RequestParam("name") String name, @RequestParam("age") Integer age) {
  User user = new User();
  user.setName(name);
  user.setAge(age);
  return userRep.save(user);
}
```

在前台通过json格式发送数据导致的。因为@RequestParam是接收表单数据的，无法识别前台发来的json格式。

```
Content-Type: application/json

{
    "name": "name",
    "age" : 12
}
```

换成表单格式就可以请求成功了。

```
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryBRi81vNtMyBL97Rb

------WebKitFormBoundaryBRi81vNtMyBL97Rb
Content-Disposition: form-data; name="name"

name1
------WebKitFormBoundaryBRi81vNtMyBL97Rb
Content-Disposition: form-data; name="age"

12
------WebKitFormBoundaryBRi81vNtMyBL97Rb--
```

## 401错误

Unauthorized。该状态码表示发送的请求需要有通过HTTP认证（BASIC认证、DIGEST认证）的认证信息。另外若之前已进行过1次请求，则表示用户认证失败。

## 403 错误

Forbidden。该状态码表明对请求资源的访问被服务器拒绝了。

## 405错误

```
"error": "Method Not Allowed",
"message": "Request method 'GET' not supported",
```

**场景：**

当发布的服务不支持当前请求形式的时候就会报405错误。比如上面这个错误是由于我通过Spring Boot发布了的服务是post的形式，而我请求的方式是get。

## 415错误

```
"error":"Unsupported Media Type",
"message":"Content type 'multipart/form-data;boundary=----WebKitFormBoundaryOTm4oeTbegDmbXAx;charset=UTF-8' not supported"
```

**场景：**

不支持的媒体类型。上面的错误是用springboot发布的服务，不支持表单形式的数据，只能解析Content-Type为application/json; charset=utf-8的数据。

```java
@RequestMapping(value = "/adduser", method = RequestMethod.POST)
@ResponseBody
public User addUser(@RequestBody User user) {
  return userRep.save(user);
}
```

## 500 错误

Internal Server ERROR。服务器错误

## 503错误

Service Unavailable。该状态码表明服务器暂时处于超负载或正在进行停机维护，现在无法处理请求。

> 参考：
>
> <https://www.jianshu.com/p/218fa50b8f6c>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jun-wang.gitbook.io/learnjava/ji-shu-xue-xi/ji-suan-ji-wang-luo/http-cuo-wu-ma-he-chu-xian-chang-jing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
