Content-Type是什么

news2024/7/6 17:58:34

目录

Content-Type是什么

获取方式

设置方式

常见类型

application/x-www-form-urlencoded

multipart/form-data

application/json

text/xml

text/html

text/plain


Content-Type是什么

Content-Type出现在请求标头和响应标头中,意思是内容类型,用于指定请求数据和响应数据的类型。客户端和服务端对不同数据类型的处理方式不同。

获取方式

可以通过HttpServletRequest对象来获取

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class DemoController {
    
    private final HttpServletRequest request;
    
    @RequestMapping("/test")
    public String demo() {
        String contentType = request.getContentType();
        System.out.println(contentType);
        return contentType;
    }
}

测试结果:

设置方式

可以通过HttpServletResponse对象来设置

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequiredArgsConstructor
public class DemoController {

    private final HttpServletResponse response;

    @RequestMapping("/test")
    public void demo() throws IOException {
        System.out.println("test success!");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/x-www-form-urlencoded;charset=utf-8");
        String value = new ObjectMapper().writeValueAsString("test success!");
        response.getWriter().write(value);
    }
}

测试结果:

常见类型

application/x-www-form-urlencoded

用于表单提交,请求方式为POST,数据以键值对的形式携带在请求体中。如果包含中文或者特殊字符,会进行URL转码。

URL转码:URL转码是将URL中的非法字符替换为特殊字符序列的过程。URL中只允许出现英文字母、数字以及部分特殊字符,而其他字符(例如中文和一些特殊字符)是不允许直接出现的。如果URL中包含了这些非法字符,就需要进行转码,将其转换为URL允许的形式。中文一般使用utf-8编码进行转码。

例如:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form method="POST" action="http://localhost:8080/test" enctype="application/x-www-form-urlencoded">
        用户名:<input type="text" name="username" />
        密码:<input type="password" name="password" />
        <input type="submit" value="登录" />
    </form>
</body>

</html>

接收方式:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @PostMapping("/test")
    public String demo(String username, String password) {
        return "username=" + username + "password=" + password;
    }
}

multipart/form-data

用于表单提交,请求方式为POST,上传文件。

例如:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form method="POST" action="http://localhost:8080/test" enctype="multipart/form-data">
        上传文件:<input type="file" name="file" />
        <input type="submit" value="提交" />
    </form>
</body>

</html>

接收方式:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class DemoController {

    @PostMapping("/test")
    public String demo(MultipartFile file) {
        return file.getOriginalFilename();
    }
}

application/json

指定数据的格式为json格式。

例如:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/axios.js"></script>
</head>

<body>
    <input type="button" value="获取数据POST" onclick="post()">
</body>

<script>
    function post() {
        axios({
            method: "post",
            url: "http://localhost:8080/test",
            data: {
                "username": "艾伦",
                "password": "123abc"
            }
        }).then(result => {
            console.log(result.data)
        })
    }
</script>

</html>

接收方式:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@Data
@NoArgsConstructor
public class UserLogin {
    private String username;
    private String password;
}
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @PostMapping("/test")
    public String demo(@RequestBody UserLogin userLogin) {
        return JSONObject.toJSONString(userLogin);
    }
}

text/xml

xml格式的数据,现在已经不用了,被json格式的数据代替。

text/html

html格式的数据

text/plain

Content-Type默认的值就是text/plain,纯文本,不做任何数据处理。

例如:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form method="POST" action="http://localhost:8080/test" enctype="text/plain">
        用户名:<input type="text" name="username" />
        密码:<input type="password" name="password" />
        <input type="submit" value="登录" />
    </form>
</body>

</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1317636.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

用PHP和HTML做登录注册操作数据库Mysql

用PHP和HTML做登录注册操作数据库Mysql 两个HTML页面&#xff0c;两个PHP,两个css,两张图片&#xff0c;源码资源在上方。 目录 HTML页面 login.html <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta nam…

THEMIS---Beta Sprint Summary Essay Blog

Which course does this assignment belong to2301-MUSE社区-CSDN社区云What are the requirements for this assignmentbeta SprintThe goal of this assignmentTo summarize the beta task progress and the teams sprintsTeam NameThemisTop-of-the-line collection of essa…

Impala4.x源码阅读笔记(一)——HdfsTextScanner解析

前言 本文为笔者个人阅读Apache Impala源码时的笔记&#xff0c;仅代表我个人对代码的理解&#xff0c;个人水平有限&#xff0c;文章可能存在理解错误、遗漏或者过时之处。如果有任何错误或者有更好的见解&#xff0c;欢迎指正。 在文章Impala3.4源码阅读笔记&#xff08;七…

带你亲证AI应用开发的“奇点”时刻

带你亲证AI应用开发的“奇点”时刻 AI 应用开发——新的历史节点 事实上&#xff0c;没有任何一种突破能够不经历重重失败&#xff0c;不体验一轮轮的痛苦&#xff0c;就能直接展现在人类面前。AI 技术自诞生之初直至今日&#xff0c;其发展之路从未一帆风顺——辉煌与寒冬交…

Windows环境下QT应用程序的发布

时间记录&#xff1a;2023/12/17 1.生成版本介绍&#xff0c;debug&#xff1a;调试版本&#xff0c;携带调试信息&#xff0c;占用内存稍大一些&#xff0c;release&#xff1a;发布版本&#xff0c;一般开发完毕选择此套件进行编译生成可执行程序进行发布 2.发布步骤 &#x…

流程引擎相关资料

最近调研流程引擎相关知识&#xff0c;BPMN规范和流程引擎相关知识无法避开&#xff0c;以及市面上比较多的流程引擎产品。 BPMN2.0 基本形状 流对象&#xff08;Flow Objects&#xff09;&#xff0c;流对象是定义业务流程的主要图形元素。它进一步细分为三个类别&#xff0…

使用邮件群发平台,轻松实现高效沟通的4大优势!

新媒体带动着众多线上平台的发展&#xff0c;使得流量为企业带来了可观的营收。但是&#xff0c;随着短视频市场的饱和&#xff0c;想要再次获得初始时的流量就变得越发困难。在这个时候&#xff0c;企业不妨将眼光往邮件群发这个传统的营销方式上倾斜&#xff0c;特别是出海、…

LangChain 27 BabyAGI编写旧金山的天气预报

LangChain系列文章 LangChain 实现给动物取名字&#xff0c;LangChain 2模块化prompt template并用streamlit生成网站 实现给动物取名字LangChain 3使用Agent访问Wikipedia和llm-math计算狗的平均年龄LangChain 4用向量数据库Faiss存储&#xff0c;读取YouTube的视频文本搜索I…

别小看Python的【print】函数,这些高级用法你知道吗?

文章目录 引言技巧1&#xff1a;格式化输出示例1&#xff1a;使用%s来插入字符串&#xff0c;使用%d来插入整数示例2&#xff1a;使用字符串的format()方法示例3&#xff1a;使用f-string格式化输出 技巧2&#xff1a;控制输出文本的颜色技巧3&#xff1a;将打印结果重定向至文…

什么是Maven?

什么是Maven 1、Maven是依赖管理、项目构建工具。 pom.xml springBoot项目的核心配置文件&#xff0c;pom项目对象模型、Dependency依赖管理模型。 Maven中的GAVP是指&#xff1a; 1、GroupId&#xff1a;当前工程组织id&#xff0c;例如&#xff1a;com.jd.tddl 2、ArtifactI…

Redis-对象

参考资料 极客时间Redis&#xff08;亚风&#xff09; Redis对象 String • 基本编码⽅式是RAW&#xff0c;基于简单动态字符串&#xff08;SDS&#xff09;实现&#xff0c;存储上限为512mb。 • 如果存储的SDS⻓度⼩于44字节&#xff0c;则会采⽤EMBSTR编码&#xff0c;此…

QT:Unable to create a debugging engine.

debug跑不了&#xff1a; 报错&#xff1a;Unable to create a debugging engine. 参考&#xff1a; https://blog.csdn.net/u010906468/article/details/104716198 先检查是否安装了DEBUG插件 工具-》》选项 查看插件&#xff0c;如果没有的话&#xff0c;需要重新安装qt时…

计算机操作系统-第十七天

目录 处理机调度 调度的基本概念 作业 高级调度&#xff08;作业调度&#xff09; 中级调度&#xff08;内存调度&#xff09; 低级调度&#xff08;进程调度/处理机调度&#xff09; 进程的挂起态与七状态模型 挂起和阻塞的区别 三层调度的联系和对比 本节思维导图…

SDN控制器-ONOS中的最终一致性存储

ONOS中的数据存储基本上都是以KV进行存储的。按照一致性强弱类型可以分为强一致性存储&#xff08;strong consistency&#xff09;与弱一致性存储&#xff08;eventually consistency&#xff09;。 比较典型的&#xff0c;如ONOS中对于设备接口的存储&#xff0c;使用的是强…

XXE漏洞 [NCTF2019]Fake XML cookbook1

打开题目 查看源代码 发现我们post传入的数据都被放到了doLogin.php下面 访问一下看看 提示加载外部xml实体 bp抓包一下看看 得到flag 或者这样 但是很明显这样是不行的&#xff0c;因为资源是在admin上&#xff0c;也就是用户名那里 PHP引用外部实体&#xff0c;常见的利用…

【23-24 秋学期】NNDL 作业11 LSTM

目录 习题6-4 推导LSTM网络中参数的梯度&#xff0c; 并分析其避免梯度消失的效果 习题6-3P 编程实现下图LSTM运行过程 &#xff08;一&#xff09;numpy实现 &#xff08;二&#xff09;使用nn.LSTMCell实现 &#xff08;三&#xff09; 使用nn.LSTM实现 总结 &#x…

PMI相关证书的获取步骤及注意内容

近几年很多行业的从业人员都在考取PMI项目管理相关证书&#xff0c;可在中国大陆地区参加考试的认证主要有&#xff1a;PMP, PgMP, PMI-RMP, PMI-ACP, PMI-PBA, CAPM。PfMP, PMI-SP尚未在中国大陆地区开放考试。 现整理该类证书的相关获取步骤及注意内容 一、证书获取步骤 S…

动态规划(Dynamic Programming)

动态规划&#xff08;Dynamic Programming&#xff09;&#xff1a;是运筹学的一种最优化方法&#xff0c;只不过在计算机问题上应用比较多 DP常见步骤&#xff1a; 暴力递归/穷举记忆化搜索&#xff08;傻缓存 递归&#xff09;,使用备忘录/ DP Table 来优化穷举过程严格表结…

锁--07_2---- index merge(索引合并)引起的死锁

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 案例分析生产背景死锁日志表结构执行计划 EXPLAN为什么会用 index_merge&#xff08;索引合并&#xff09;为什么用了 index_merge就死锁了解决方案注&#xff1a;M…

SQL基础:操作环境搭建

在上一节中&#xff0c;我们简单讲述了数据库和SQL的基本概念。 本节我们讲述一下环境搭建&#xff0c;为下一节讲表的基本操作做下铺垫。 环境搭建 具体到操作&#xff0c;我们就要准备一些环境了。如果不进行练习&#xff0c;我们学习的知识将很快被遗忘。 MySQL安装&…