Using Charles to simulate HTTP requests and responses

Source: Internet
Author: User

The latest project group service side of the children's shoes are too not to force, the interface has not come out, in order to facilitate the development. The server's children's shoes define the HTTP interface and the JSON structure that is returned. Then the client can take advantage of the JSON structure, simulate the data, and then successfully develop, and finally, when the server interface completes, again do a joint debugging.

Such a method, there is a professional name mock.

Here's how to take advantage of mock HTTP responses.

Like there's an HTTP interface.

Http://xx.oo.com/api/helloworld

The JSON structure returned is as follows.

{  "Hello": "World"}
We can use the following Web site to simulate a JSON response like this.

http://dtjson.com/


Clicking Generate my HTTP Response generates a URL that accesses the URL to return the JSON string you need. Above I define the JSON, generate the URL below, click on Access, you can get.

http://dtjson.com/v2/54c76c669663e6eb041a7281


At this point, our false interface has been completed, then in the client code is to replace the Http://xx.oo.com/api/helloworld to http://dtjson.com/v2/54c76c669663e6eb041a7281 it.

Of course not, we can use a tool, Charles, which can proxy Android simulator or mobile phone, listen to all the HTTP access, and it also has the ability to modify the HTTP request and response, the function is very powerful.


The specific Charles Introduction, here do not introduce, you can refer to http://blog.chinaunix.net/uid-21633169-id-4329277.html, which describes in detail the main use of Charles.


In the Android simulator Http://xx.oo.com/api/helloworld, is not responding, because this is my random URL, then we use Charles to mock request


Here is the result of the HTTP request intercepted by Charles, where we can look at the HTTP protocol, including the request line, the header file, and so on. However, the response label is blank because the request is not responding.



We set this request, right click on the request, the following menu appears, we select Map Remote. Then fill in the proxy address I need, click OK, and then revisit.


And then you can have a smooth visit.





At this point, all the steps to simulate the request data have been completed. In the actual development, as long as the simulation test, you can successfully skip the wait for the interface steps, continue to work, wait until the interface completes, remove the remote map settings, you can access the normal interface.



Simulate HTTP requests and responses with Charles

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.