Alternate ways for Logging the Response Object in Golang - http

I am making a logging package for microservices in Go.
I am new to Go and was not able to find any alternate method for properly logging the response body end to end.
It goes like-
func(w http.ResponseWriter, r *http.Request)
and
http.get("some url to hit the API")
I have picked up my testing in an open github project where I have the first way which helped me logging the request object.
I am not sure if I can use the same method the get the whole response object with status code,time, response and other usual configs which I can retrieve from.
I have to make a json string from the object and send it to an index I created on elasticsearch.
Is there a way to get an response object saved without knowing which url was being hit?
Please let me know if any other details are needed.
I am using gorilla/mux for setting up my server.

Related

Is GitHub CLI "--jq" argument (to select values from the response) processed on the server or on the client?

according to examples from https://cli.github.com/manual/gh_api:
# print only specific fields from the response
$ gh api repos/{owner}/{repo}/issues --jq '.[].title'
GitHUb CLI (gh) can use the '-q' or '--jq' argument followed by a query string to select values from the response and e.g. display only certain fields.
I'd like to do a similar thing using either CURL, postman or JavaScript fetch().
So my question is: is the --jq query string sent (somehow) by gh CLI as part of an http request and processed on the server (to reduce the amount of data in the response), or is it only applied on the client side as post-processing of the data received ?
And if that query string can be passed to and processed on the server, how should it be specified if I need to make the request not with gh but with curl, postman or javascript fetch() method?
I fear the processing is actually done on the client, meaning the http response will always provide the full data...
Can someone confirm this?
Thanks!
The response is filtered on the client. You can verify yourself by taking a look at the source code:
if opts.FilterOutput != "" && serverError == "" {
// TODO: reuse parsed query across pagination invocations
err = jq.Evaluate(responseBody, bodyWriter, opts.FilterOutput)
if err != nil {
return
}
}
curl, postman, fetch
Your goal is to make an HTTP Request, not use the gh CLI.
GitHub's GraphQL API is what you seek:
To create integrations, retrieve data, and automate your workflows, use the GitHub GraphQL API. The GitHub GraphQL API offers more precise and flexible queries than the GitHub REST API.
GraphQL only returns the data requested. The data is "processed" on the API server and then sent to the client.

Is there a way to set the http Header values for an esp_https_ota call?

I'm trying to download a firmware.bin file that is produced in a private Github repository. I have the code that is finding the right asset url to download the file and per Github instructions the accept header needs to be set to accept: application/octet-stream in order to get the binary file. I'm only getting JSON in response. If I run the same request through postman I'm getting a binary file as the body. I've tried downloading it using HTTPClient and I get the same JSON request. It seems the headers aren't being set as requested to tell Github to send the binary content as I'm just getting JSON. As for the ArduinoOTA abstraction, I can't see how to even try to set headers and in digging into the esp_https_ota functions and http_client functions there doesn't appear to be a way to set headers for any of these higher level abstractions because the http_config object has no place for headers as far as I can tell. I might file a feature request to allow for this, but am new to this programming area and want to check to see if I'm missing something first.
Code returns JSON, not binary. URL is github rest api url to the asset (works in postman)
HTTPClient http2;
http2.setAuthorization(githubname,githubpass);
http2.addHeader("Authorization","token MYTOKEN");
http2.addHeader("accept","application/octet-stream");
http2.begin( firmwareURL, GHAPI_CERT); //Specify the URL and certificate
With the ESP IDF HTTP client you can add headers to an initialized HTTP client using function esp_http_client_set_header().
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_http_client_set_header(client, "HeaderKey", "HeaderValue");
err = esp_http_client_perform(client);
If using the HTTPS OTA API, you can register for a callback which gives you a handle to the underlying HTTP client. You can then do the exact same as in above example.

Can I have a body and a file in the Form-Data of a http request

I'm working in a Go Lang REST API repo.
I'm wanting to build an endpoint that will take in a file (as part of the form-data, so I suppose I'll use request.FormFile('my-file-key')). This endpoint should also take in a body of a JSON model (which i suppose would be decoded with something like this:
var myData model.MyModel
json.NewDecoder(request.Body).Decode(&myData)
But I'm running into a lot of issues. Is it even possible to send both a body and a file in the form-data with a http request?
If I try to send both I get errors from FormFile saying that it can't find the file of the key name (but if I send the exact same request without a body, this error doesn't happen). I guessing it's having trouble decoding the request.
What you need is a multipart request. One part can be JSON data, and the other part the file data.
If you're using a Go client to prepare the request, you need to use the mime/multipart package to create a Writer, then use CreatePart to create the JSON part, then the file part, and submit the request to the server.
On the decoding side: since the body is JSON you cannot parse it as a form. You have to use a multipart.Reader to read from the body after you parse the headers. Again, from that reader you get a Part, and read the data from that part. You'll get two parts, one for the JSON data and one for the file data.

How do I send a redirect as response from onCall function

I'm trying to implement paypal-rest-sdk using a direct call from the application, using onCall(). According to the documentation, the answer must be a JSON object, which will include the data returned by the server. But what happens if I want to send a redirect as a response, such as when using onRequest((req, res) res.redirect(302, url)?

Amazon Dynamodb Exception error

when we are calling dynamodb with http rest api it is giving this error
Can i know what is the problem? what are all the required things we need to append in the dynamodb url??
http://dynamodb.us-east-1.amazonaws.com/?aws_access_key=XXXXXXXXXXXXXXXX&aws_secret_access_key=ZZZZZZZZZZZZZZZZZZZZZZ
Do we need to append anything more parameters with this url please let me know??
http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/UsingJSON.html#JSONMajorExample
Your solution is in the same link
http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/MakingHTTPRequests.html
If you don't use one of the AWS SDKs, you can perform Amazon DynamoDB operations over HTTP using the POST request method. The POST method requires you to specify the operation in the header of the request and provide the data for the operation in JSON format in the body of the request.
You need to make POST request with all the required parameters mentioned in that page.

Resources