Use Restlet client to send various get and POST requests

Source: Internet
Author: User

When you are developing a Web app, you need to send a variety of get and post requests for testing when you test the controller in spring, and you can, of course, enter the URL yourself in the browser or test it with the write code that spring provides. MockMvc But when we want to test a request such as post with Form form submission (submit file), enter the URL directly in the browser or use MockMvc (this I do not know how to do ...) is not very good to achieve, Restlet client provides us with convenience.
The address is: https://restlet.com/modules/client/, can install Chrome browser plug-in, so it is more convenient to use, plug-in browser interface:

There are three main sections, the request section is used to create the requests, the response section represents the requested reply, which can be
The complete Request Headers section looks at the full header of the request that you made, and then creates the request

1. Create a normal GET request

Sending a GET request is simple, just enter the URL and add the parameters.

2. Create a normal POST request
Sending a normal post request is similar to sending a GET request in 1, just changing the method to post and adding the necessary parameters

3. Create a POST request to simulate form submission forms
Sometimes we need to simulate the form submission file, and the corresponding controller code can be obtained as follows

@RequestMapping ("/proj") @ResponseBody PublicAjaxresult Upload (StringFileName, InputStream InputStream, HttpServletRequestRequest) {Multiparthttpservletrequest multirequest = (multiparthttpservletrequest)Request;intFileCount =0; iterator<String> iterator = Multirequest.getfilenames (); while(Iterator.hasnext ()) {StringFormfileelementname = iterator.Next();                        filecount++; Multipartfile multipartfile = Multirequest.getfile (formfileelementname);if(NULL= = Multipartfile) {throwNewException ("File cannot be empty"); }                    }        }
  
 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

When we send a request for a mock form form, we notice that the request is actually a MultipartHttpServletRequest type, andspring automatically populates the InputStream parameter, and in fact he is the same as the InputStream in the request. The restlet can be simulated as follows

There are a few places to be aware of:

    • BODYOption inside the selectionForm
    • Click Add form parameter and select Type File , and Note that you must write the file name, otherwise the controller code Iterator<String>
      iterator = multiRequest.getFileNames();
      is not to get the file name
    • Note Content-Type the type is multipart/form-data
      We looked at the complete request HEADERS below to see that our request type is Multi/form-data, while Restlet client automatically sets the header for US boundary

      4. Create a POST request and transfer files directly
      In addition to simulating form submission files, we can also add files directly to the body, when the controller's request is not a MultipartHttpServletRequest type

      Select the file option in body and drag and drop files, and Restlet client will automatically fill in the corresponding Content-type type.

Use Restlet client to send various get and POST requests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.