1. Overview
1.概述
In this tutorial, we’ll explore the ways to send an array as part of the x-www-form-urlencoded using Postman.
在本教程中,我们将探索使用Postman发送数组作为x-www-form-urlencoded的一部分的方法。
The W3C committee has defined multiple formats we can use for sending data over the network layer. These formats include the form-data, raw and x-www-form-urlencoded data. We’re sending data using the latter format by default.
W3C委员会已经定义了多种格式,我们可以用来在网络层上发送数据。这些格式包括form-data、raw和x-www-form-urlencoded数据。我们在发送数据时默认使用后者的格式。
2. x-www-form-urlencoded
2.x-www-form-urlencoded
Listed format describes form data sent as one block in the HTTP message body. It sends an encoded form data set for submission to the server. The encoded data has the format of key-value pairs. The server must support the content type.
列出的格式描述了在HTTP消息正文中作为一个块发送的表单数据。它发送了一个编码的表单数据集,以便提交给服务器。编码后的数据具有键值对的格式。服务器必须支持该内容类型。
Forms submitted using this content type match the following encoding pattern:
使用这种内容类型提交的表格符合以下编码模式。
- The control names and the values are escaped.
- The ‘,’ symbol will separate multiple values.
- The ‘+’ symbol will replace all space characters.
- Reserved characters should follow the RFC 17.38 notations.
- All non-alphanumeric characters are encoded using percent encoding.
- The key is separated from the value with the equal symbol (‘=’), and key-value pairs are separated from each other with an ampersand (‘&’).
In addition, the length of the data is not specified. However, using the x-www-form-urlencoded data type has a data limit. Therefore, the media server will reject requests that exceeded the size specified inside the configuration.
此外,数据的长度也没有规定。然而,使用x-www-form-urlencoded数据类型有一个数据限制。因此,媒体服务器将拒绝超过配置内指定大小的请求。
Moreover, it became inefficient when sending binary data or values containing non-alphanumeric characters. Keys and values containing non-alphanumeric characters are percent-encoded (also known as URL encoding), so this type is not suitable for binary data. Therefore, we should consider using the form-data content type instead.
此外,在发送二进制数据或包含非字母数字字符的值时,它的效率变得很低。包含非字母数字字符的键和值是percent-encoded(也称为URL编码),所以这种类型不适合二进制数据。因此,我们应该考虑使用form-data内容类型来代替。
Furthermore, we cannot use it for encoding files. It can only encode the URL parameters or the data inside the request body.
此外,我们不能用它来编码文件。它只能对URL参数或请求体内部的数据进行编码。
3. Sending an Array
3.发送一个数组
To use the x-www-form-urlencoded type in Postman, we need to select the radio button with the same name within the request’s body tab.
要在Postman中使用x-www-form-urlencoded类型,我们需要在请求的body标签中选择具有相同名称的单选按钮。
As already mentioned, the request consists of the key-value pairs. Postman will encode the data before sending it to the server. Additionally, it will encode both the key and the value.
如前所述,请求由键值对组成。Postman将在发送数据到服务器之前对其进行编码。此外,它将对键和值都进行编码。
Now, let’s see how to send an array in Postman.
现在,让我们看看如何在Postman中发送一个数组。
3.1. Sending Simple Array Object
3.1.发送简单数组对象
We’ll start by showing how to send a simple array object that contains simple object types, for instance, String elements.
我们将首先展示如何发送一个简单的数组对象,其中包含简单的对象类型,例如,String元素。
First, let’s create a Student class that will have an array as an instance variable:
首先,让我们创建一个Student类,它将有一个数组作为实例变量。
class StudentSimple {
private String firstName;
private String lastName;
private String[] courses;
// getters and setters
}
Second, we’ll define a controller class that will expose the REST endpoint:
第二,我们将定义一个控制器类,它将暴露REST端点。
@PostMapping(
path = "/simple",
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public ResponseEntity<StudentSimple> createStudentSimple(StudentSimple student) {
return ResponseEntity.ok(student);
}
When we use the consume attribute, we need to define x-www-form-urlencoded as a media type that the controller will accept from a client. Otherwise, we’ll get the 415 Unsupported Media Type error. Additionally, we need to omit the @RequestBody annotation since the annotation does not support the x-www-form-urlencoded content type.
当我们使用consumer属性时,我们需要将x-www-form-urlencoded定义为控制器将从客户端接受的媒体类型。否则,我们将得到415 Unsupported Media Type错误。此外,我们需要省略@RequestBody注解,因为该注解不支持x-www-form-urlencoded内容类型。
Finally, let’s create a request in Postman. The simplest way is to separate values using the comma symbol (‘,’):
最后,让我们在Postman中创建一个请求。最简单的方法是使用逗号(‘,’)来分隔数值。
However, this approach may cause a problem if the value contains the comma symbol itself. We can resolve the problem by setting each value separately. To set elements to the courses array, we need to provide the key-value pairs using the same key:
然而,如果值本身包含逗号符号,这种方法可能会引起问题。我们可以通过单独设置每个值来解决这个问题。为了给courses数组设置元素,我们需要使用相同的键来提供键值对。
The order of the elements in an array will follow the order provided in the request.
数组中元素的顺序将遵循请求中提供的顺序。
Additionally, the square brackets are optional. On the other hand, if we want to add an element to the specific index in the array, we can achieve this by specifying the index in the square brackets:
另外,方括号是可选的。另一方面,如果我们想向数组中的特定索引添加一个元素,我们可以通过指定方括号中的索引来实现。
We can test our application using the cURL request:
我们可以使用cURL请求来测试我们的应用程序。
curl -X POST \
http://localhost:8080/students/simple \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'firstName=Jane&lastName=Doe&
courses[2]=Programming+in+Java&
courses[1]=Data+Structures&
courses[0]=Linux+Basics'
3.2. Sending Complex Array Object
3.2.发送复杂数组对象
Now, let’s take a look at the way of sending an array containing complex objects.
现在,让我们来看看发送包含复杂对象的数组的方式。
First, let’s define the Course class that will represent a single course:
首先,让我们定义Course类,它将代表一个单一的课程。
class Course {
private String name;
private int hours;
// getters and setters
}
Next, we’ll create a class that will represent a student:
接下来,我们将创建一个代表学生的类。
class StudentComplex {
private String firstName;
private String lastName;
private Course[] courses;
// getters and setters
}
Let’s add a new endpoint inside the controller class:
让我们在控制器类中添加一个新的端点。
@PostMapping(
path = "/complex",
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public ResponseEntity<StudentComplex> createStudentComplex(StudentComplex student) {
return ResponseEntity.ok(student);
}
Finally, let’s create a request in Postman. As well as in the previous example, to add elements to the array, we need to provide the key-value pairs using the same key:
最后,让我们在Postman中创建一个请求。和前面的例子一样,为了向数组添加元素,我们需要使用相同的键来提供键值对。
Here, the square brackets with indexes are mandatory. To set the value to each instance variable, we need to use the dot (‘.’) operator followed by the name of the variable.
这里,带索引的方括号是强制性的。为了给每个实例变量设置值,我们需要使用点(’.’)运算符,后面跟着变量的名字。
Again, we can test the endpoint using the cURL request:
同样,我们可以使用cURL请求来测试端点。
curl -X POST \
http://localhost:8080/students/complex \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'firstName=Jane&lastName=Doe&
courses[0].name=Programming+in+Java&
courses[0].hours=40&
courses[1].name=Data+Structures&
courses[1].hours=35'
4. Conclusion
4.总结
In this article, we’ve learned how to adequately set Content-Type on the server side to avoid the Unsupported Media Type error. Also, we’ve explained how to send both simple and complex arrays using the x-www-form-urlencoded content type in Postman.
在这篇文章中,我们学习了如何在服务器端充分设置Content-Type,以避免出现Unsupported Media Type错误。此外,我们还解释了如何在Postman中使用x-www-form-urlencoded内容类型发送简单和复杂的数组。
As always, all the code snippets can be found over on GitHub.
一如既往,所有的代码片段都可以在GitHub上找到。