I am creating a command line utility using go , which will request particular server for data and will get the data from server in JSON response. Utility can do multiple requests for multiple products . I am able to do multiple requests and get the response for each product properly in JSON format. Now , I want to save the result locally in caching or local files. By which on every request I will check the local cache before sending request to server . If data is available for that product then no request will be send .
I want to save the whole json response in cache or local file and keep that data every time before doing any request to server for data.
Use Case : Products {"A","B","C","D","E"} It could be any number of products Test 1 : Get data for A,B,C Check local storage whether data is available or do request. Save json request in storage for each product.
So for test 1 ,It will have entry like: [{test 1 : [{product a : json response} , {product b : json response} ,{product c : json response}]}]
And in case if test fails in between and it get results for two products it should save response for 2 products and if we reran the test it will get result for 3rd product only.
There's a bunch of Go libraries to do HTTP caching transparently.
Choose the one you like most and satisfies your needs better, both have examples in their README to get you started real fast.
If for some reason you can't or don't wanna use third-party libraries check out this answer https://stackoverflow.com/a/32885209/322221 which uses httputil.DumpResponse and http.ReadResponse, both on Go's standard library and also the answerer provides an example implementation you can base your work on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With