Sending SOAP Request via Postman – 通过Postman发送SOAP请求

最后修改: 2022年 8月 16日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we’re going to send a SOAP request via Postman. Before that, we’ll import the WSDL from our Country SOAP service into the API platform.

在本文中,我们将通过Postman发送一个SOAP请求。在此之前,我们将把WSDL从我们的Country SOAP服务导入到API平台。

2. Setup

2.设置

Before we can issue SOAP requests in Postman, we’ll need a functioning SOAP service. After running our Country SOAP service, the endpoint will be located at http://localhost:8080/ws, and the WSDL can be found at http://localhost:8080/ws/countries.wsdl.

在我们能够在Postman中发出SOAP请求之前,我们需要一个有效的SOAP服务。在运行我们的Country SOAP service之后,端点将位于http://localhost:8080/ws,以及WSDL可以在http://localhost:8080/ws/countries.wsdl>找到。

3. Testing SOAP Request from Postman

3.测试来自Postman的SOAP请求

There are four steps to test our endpoint with Postman.

有四个步骤可以用Postman测试我们的端点。

3.1. Import SOAP WSDL

3.1 SOAP WSDL导入

Since Postman 8.4.0, we can import our WSDL into Postman. We can directly import our countries Postman collection. Here are few steps to create a new collection from the WSDL.

自Postman 8.4.0以来,我们可以将我们的WSDL导入Postman。我们可以直接导入我们的countries Postman集合。这里有几个步骤可以从WSDL中创建一个新的集合。

First, let’s click on Collections:

首先,让我们点击Collections

1

Next, let’s import our WSDL by providing its URL:

接下来,让我们通过提供它的URL来导入我们的WSDL。

2

You can also import by directly using the countries.wsdl WDSL file.

你也可以通过直接使用countries.wsdl WDSL文件导入。

Our services have been fetched from the WSDL. We’ll skip advanced settings and import with the defaults:

我们的服务已经从WSDL中获取了。我们将跳过高级设置,用默认值导入。

3

After importing, we should be able to see all our SOAP services:

导入后,我们应该能够看到我们所有的SOAP服务。

3.2

Postman has taken care of setting the right URL, content type, and headers for each request.

Postman已经注意到为每个请求设置正确的URL、内容类型和头文件。

3.2. Add Body Data

3.2.添加主体数据

Next, let’s customize our request body by adding Spain as country name and the baeldung namespace in the envelope header:

接下来,让我们通过在信封头中添加西班牙作为国家名称baeldung命名空间来定制我们的请求正文。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:gs="http://www.baeldung.com/springsoap/gen">
    <soapenv:Header/>
    <soapenv:Body>
        <gs:getCountryRequest>
            <gs:name>Spain</gs:name>
        </gs:getCountryRequest>
    </soapenv:Body>
</soapenv:Envelope>

3.3. Set Request Headers

3.3.设置请求头文件

By importing our WSDL, Postman has already set appropriate headers for us. The Content-Type is set to text/xml and works for our request. text/xml is preferable to application/xml. MIME user agents (and web user agents) that do not have explicit support for text/xml will treat it as text/plain, for example, by displaying the XML MIME entity as plain text.

通过导入我们的WSDL,Postman已经为我们设置了适当的头文件。Content-Type被设置为text/xml并适用于我们的请求。text/xmlapplication/xml更合适。没有明确支持text/xml的MIME用户代理(和网络用户代理)会将其视为text/plain,例如,将XML MIME实体显示为纯文本。

If a request needs another content type, we can deselect the Content-Type header automatically added by Postman. Then, we add a new row with Content-Type in the Key field and our new content type name in the Value field.

如果一个请求需要另一种内容类型,我们可以取消选择Postman自动添加的Content-Type头。然后,我们添加一个新行,在Key字段中添加Content-Type,在Value字段中添加我们的新内容类型名称。

If the service returns a status code of 500, we should add an additional header “SOAPAction: #POST”.

如果服务返回的状态代码是500,我们应该添加一个额外的标头”SOAPAction。#POST”

3.4. Send SOAP Request

3.4.发送SOAP请求

Finally, let’s hit the Send button to make our call to the SOAP service. If our call is successful, Postman displays the response containing information about Spain in the lower tab:

最后,让我们点击Send按钮,对SOAP服务进行调用。如果我们的调用是成功的,Postman会在下面的标签中显示包含西班牙信息的响应。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
    <ns2:getCountryResponse xmlns:ns2="http://www.baeldung.com/springsoap/gen">
        <ns2:country>
            <ns2:name>Spain</ns2:name>
            <ns2:population>46704314</ns2:population>
            <ns2:capital>Madrid</ns2:capital>
            <ns2:currency>EUR</ns2:currency>
        </ns2:country>
    </ns2:getCountryResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Here’s the output in the Postman console:

下面是Postman控制台的输出。

4

4. Conclusion

4.总结

In this article, we learned how to send a SOAP request via Postman. We first saw how to import our WSDL into Postman. Then, we successfully sent a request to our country service. As always, the code is available over on GitHub.

在这篇文章中,我们学习了如何通过Postman发送一个SOAP请求。我们首先看到了如何将我们的WSDL导入到Postman中。然后,我们成功地向我们的国家服务发送了一个请求。像往常一样,代码可以在GitHub上找到