preface

Only a bald head can be strong.

Star: github.com/ZhongFuChen…

I believe that we have used POSTMAN, back-end in the development of the interface to write always go to the call, see if it does not meet their expectations.

Because our interface may have a lot of parameters or our parameters are in JSON format (etc.), it is not convenient to request directly from the browser, and the browser request cannot be reused (there is no save function).

So we often use other software (plug-ins) and POSTMAN is the most famous one here.

This post is not about POSTMAN, but our favorite IDEA for Java programmers, which does what POSTMAN does, and I think it’s much better.

episode

When you switched to a MacBookPro a few days ago, you naturally needed to move your hosts configuration on Windows to the MacBookPro

Six months after graduation, I bought a MacBook Pro

So I copied the Hosts on Windows to my gas Mask through QQ (a software for managing hosts on Mac, WHICH I think is quite useful).

I happily copied these configurations into the Gas Mask. After the replication, I went to access my own pre-delivery environment is also successful (I generally need to configure hosts to access the pre-delivery environment).

But when I used POSTMAN and HTTP Clint of IDEA to test my own interface, it was 404 all the time. I couldn’t understand why 404 happened

  • I have no problem accessing the pre-release environment directly from the browser on my own machine
  • I put the same URL on Windows and it’s accessible, but on the Mac it’s not
  • Very strange ah….

So I called my little friend to help me see what the situation (I have not encountered this problem), the little friend also gave me analysis (make make a group of little friends around me to see this problem)

  1. Check minegas maskIs there any problem with the configuration?----I don’t see a problem
  2. Check if my binding has the same domain name----I don’t see a problem
  3. Check whether there are any problems with the hosts file on the host. Shortcut:Option + command + spaceAnd thencommand+shift+g, the input/etc/hostsCheck the file----I don’t see a problem
  4. pingThe domain name----I found it can ping, but it’s not my bound IP
  5. Suspected DNS pollution, to search a few commands to refresh DNS----nothing
  6. Then a voice said,Your hosts file has special characters? “I am skilled iniTerm2The inputsudo vim /etc/hosts, a look, a lot of special characters.----Problem resolved, special characters exist when copying. Ouch.

This section describes the HTTP Client

Note: Environment: IDEA 2019.3

As mentioned above, IDEA already has a tool similar to POSTMAN for us to use. If it is good enough, I don’t need to download POSTMAN.

First, let’s find its entrance:

In Tools >HTTP Client >Test RESTful Web Service, it is not difficult to find

The REST Client can be found at the bottom:

IDEA does not recommend using the REST Client. Instead, use the new HTTP Client. From the REST Client’s style, we can see that it is not much different from POSTMAN (functionally similar).

Click on the blue word to jump to the new HTTP Client recommended for IDEA.

I like this way of writing (personally). Let’s try a few links on the right to get started quickly.

First, Add Request. This is to Add a common Request template (for you to choose from).

Let’s try one of these and see what it looks like:

Then we can click on the Examples link to see what it does:

We can view various examples, which are very convenient for us to get started:

Better yet, we can define variables (different environments use different variables), and we can define http-client.env.json to write some generic variable related values. Then we can reference it in the.http file:

/ / HTTP client. The env. Json style {" dev ": {" baseUrl" : "http://gateway.xxx.cn/", "username" : ""," password ":" "}, "pre" : { "baseUrl": "http://localhsot:8888/", "username": "", "password": "" } }Copy the code

Use {{condition}} in the.http file to refer to our common environment variables:

GET {{baseUrl}}/api/item? id=99 Accept: application/jsonCopy the code

When executing, we can choose different environments to execute:

The last

What we can see is that IDEA allows us to build our own interfaces as files, which means that we can upload files to Git to facilitate collaboration.

It can be configured to help us quickly switch the corresponding environment (variable), as long as the request body is written once, can be used in multiple environments.

We can also quickly learn how to use the official Examples without any learning costs.

There are also some advanced features…. I will not say (because I do not understand), you can be interested in can go to see.

References:

  • Github.com/corningsun/…
  • Segmentfault.com/a/119000002…

When I wrote this article, Obin and Egg both said, “POSTMAN is the best tool in the world. I’m going to report him when he posts.”

Note: I’m not a deep user of POSTMAN, and the HTTP Client is good enough for me right now.

This has been included in my GitHub featured articles, welcome to Star: github.com/ZhongFuChen…

Happy to export dry Java technology public number: Java3y. The public account has more than 300 original technical articles, massive video resources, beautiful brain map, attention can be obtained!

Thank you very much talent can see here, if this article is written well, feel “three crooked” I have something to ask for praise for attention ️ to share 👥 to leave a message 💬 for warm male I really very useful!!

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see you in the next article!