Can I mirror request data with Charles Proxy? - charles-proxy

The mirror feature in Charles Proxy saves the responses -- I want to see the requests that are made for the corresponding saved response outside of the Charles UI so that I can programmatically match some of the data in the request to the response.
For instance, I might have a request to mysite.com/data?param=123 that results in the file data with the response value {"param": 123}. Then I might have another request mysite.com/data?param=456 and the same file data with a different response value.
I want to match the original request to the saved response.

You can use the Auto Save feature of Charles.
In "Charles - Tools - Auto Save", enable this feature and take "Save type" as "JSON Session File":
Then, all requests and corresponding responses will be saved as JSON data, which is pretty good for programming.

Related

ASP.NET Core Web API - PUT vs PATCH

I've been learning how to build an ASP.NET Core Web API using the official documentation. However, under the PUT section it says:
According to the HTTP specification, a PUT request requires the client to send the entire updated entity, not just the changes. To support partial updates, use HTTP PATCH.
I know that POST is used to create a new resource and PUT is used to update a particular resource. But I cannot understand what is meant by "entire updated entity, not just the changes".
I cannot understand what is meant by "entire updated entity, not just the changes".
You should review the HTTP specification, which describes the semantics of PUT.
A successful PUT of a given representation would suggest that a subsequent GET on that same target resource will result in an equivalent representation being sent in a 200 (OK) response.
Representation here refers to the bytes that would appear as the payload of the GET request. For a web page, we're talking about the HTML. For a resouce that responds with application/json, we're talking about the entire json document.
In other words, if you want to create/edit a json document, and save it on the web server, then you use HTTP PUT, and the payload of the request should be the entire JSON document.
For cases where the JSON document is very big (much bigger than the HTTP headers) and the changes you are making are small, then you might instead choose to send the changes in a patch request, where the payload of the request is a patch document (probably JSON Patch or JSON Merge Patch
Now, here's the magic trick -- everybody on the web understands HTTP requests the same way. PUT always means PUT, PATCH always means PATCH. So you can use PUT and PATCH to make changes to things on the web that aren't really documents, but might instead be something else (like an entity).
That's pretty much the point of an HTTP API - it's a facade that (when seen from the outside) makes our service look like just another web server.
The fact that your resource is really rows in a database instead of a document on a file system is an implementation detail that is hidden behind the Web API.
Supposed you have a json Data like below
{
"Id":123,
"FirstName":"John",
"MiddleName":"Random",
"LastName":"Doe"
}
Suppose, you want to change middle name here. you want to remove it. You can use both put and patch to do so.
PUT
Here you've to send the whole new entity (the changed state of the json model) in your request. Though you're only changing the Middle name part, the full json data is being sent in the request payload
PUT /user?id=123
{
"Id":123,
"FirstName":"John",
"MiddleName":"", //Only this is being changed.
"LastName":"Doe"
}
PATCH
In this case, you can only send the diff between the old model and the new model, and that will be it. So, here, in this case, only the changes are being sent, not the whole updated entity
PATCH /user?id=123
{
"MiddleName":"", //Only this is being changed.
}
Using PATCH can be handy when you want to edit only a few properties of a huge object.

Cache response body from http request in flutter

I am looking for a way to cache the response from a http get request and everytime the user opens the app i want to compare the data in the cache with the data from the request and display the http response instead of the cached data if there is any difference
You should try using the package flutter_cache_manager, that should fit your needs.

REST API Testing: How to get response using Google Chrome developer tools?

I'm very new to API testing.
I'm trying to make use of Google Chrome's developer tools to understand and explore this subject.
Question 1:
Is it possible to get the response (possibly in JSON format) of a simple GET request using chrome developer tools?
What I'm currently doing is:
Open chrome developer tools
Go to Network tab
Clear existing logs
Send a post request simply by hitting a URL. e.g. https://stackoverflow.com/questions/ask
Check the corresponding docs loaded
Question 2:
What are the relevance "Reponse Headers" shown on the image above? I mean, am I correct to think that this is the response I am getting after doing the GET request?
Any help or references you can give are much appreciated!
If you want to test a rest api I sugest you get postman which is meant for that purpose.
Going to your questions:
Question 1: Is it possible to get the response (possibly in JSON
format) of a simple GET request using chrome developer tools?
The first point to make clear is that it is the server who will or will not send a json response to the browser. Not the browser who can choose to see any response as json.
If you send a GET request that the server responds with a json object or json array and the Content-type header is set to application/json, you will see that response already formated in the main window of the browser.
If the Content-type is set to text/html, for example, then you will still get the a json text as response in the main window but it won't be nicely formated. Depending on how the response was sent, sometimes you can see it nicely formatted by left clicking the browser window and selecting view source page.
For this you don't need developer's tools unless you want to see how long did it take to receive the response, or check the headers for some specific value, etc, but nothing to do with receiving the response or rendering it on screen.
Developer's tools is more usefull if you are working with javascript/jquery and/or if you are sending ajax requests (GET or POST). In these cases you can debug the function and also see the ajax request to check what actually went out from your browser and what was received as a response.
Question 2: What are the relevance "Reponse Headers" shown on the
image above? I mean, am I correct to think that this is the response I
am getting after doing the GET request?
In the response you get the two things, the headers, and the content. The json objects you see are part of the content not the headers.
The headers will tell the browser, for example, that the body is json (vs. an html documenet or something different), besides of other information like cache-control, or how long the body is.
Search for http headers for more information on which are teh standard headers.
To answer your questions narrowly:
Is it possible to get the response (possibly in JSON format) of a simple GET request using chrome developer tools?
Yes! Just click the Response tab, which is to the right of the Headers tab that's open in your screenshot.
What are the relevance "Reponse Headers" shown on the image above? I mean, am I correct to think that this is the response I am getting after doing the GET request?
Yes, these are the HTTP headers that were sent with the response to your request.
The broader question here is "how do I test a REST API?" DevTools is good for manual testing, but there are automated tools that can make it more efficient. I'll leave that up to you to learn more about that broad topic.

http POST vs GET request for getting a document sending a XML file

I know for getting a data we have to use a GET request. But this time I have to send a XML document (who will not be stored) for getting the data. What are the best practice in this case ?
You need send a xml document to your end-point to get your interested data.
As you need a xml doc, it has to be a POST REST end-point. (On a side note, sending any file contents as part of GET parameters is a bad design practice.)

specify response for fiddler core

I use fiddler core to intercept the request and provide a response to it. I know its possible to use saz files to save the response. But the problem is that I need to be able to customize the response. While its a saz file I cant customize the response manually.
Is there a way to save response caught by fiddler to a text file in json like format, so that I could edit it and could serve it as response to any request using fiddler core? For now i see I can save response as a plain text. But how do I load this request to fiddler or parse it with fiddler core to populate all the response properties? Is there some format I could use, that will allow me to manually edit the response?
UPDATE
I see I can just open saz archive, make my edits to reponse and use it to specify the response. Thats exactly what I was looking for. Also there is a way to save response session as har file. Is it possible to save one single response as har/saz file? Currently I can only save session and it contain all requests and responses. Is there a way to limit saved data to 1 request and 1 response?
You have a SAZ file, which contains the full content of a response. Your code may load the SAZ File into FiddlerCore using the Utilities.ReadSessionArchive method. You will then have an array of Session objects.
As FiddlerCore receives requests, you can evaluate whether or not you wish to reply to each request using a previously-loaded response or whether you want to instead let the request flow through to the server. To let the request flow through to the server, do nothing.
To return a previously-generated response, in FiddlerCore 2.4.6.4+ (not yet released), simply call utilAssignResponse on the new Session. For earlier versions of FiddlerCore without this new method, your OnBeforeRequest method should call a method that looks something like this:
public void utilAssignResponse(Session oS, HTTPResponseHeaders oRH, byte[] arrBody)
oS.utilCreateResponseAndBypassServer();
oS.oResponse.headers = (HTTPResponseHeaders)oRH.Clone();
oS.responseBodyBytes = arrBody ?? Utilities.emptyByteArray;
oS.oFlags["x-Fiddler-Generated"] = "Generated by myCode";
}