can resolveWithFullResponse: true make response heavier network-wise? - npm

https://github.com/request/request-promise
I want to measure and possibly minimise the payload of response.
So I want to get content-length header. I will not get header unles i specify resolveWithFullResponse to request. The question is, how it works, in detail? Can it make server response larger in terms of network load?
When i get specific page with browser, i got different (3 times smaller, in my case) content-length header value, compared to getting the same page with request-promise with resolveWithFullResponse: true.
So is request-promise just trim response it gets, if you do not specify resoveWithFullResponse, or it requests more data in case it is set?

resolveWithFullResponse just means that instead of receiving the response body in your .then, you'll receive the full IncomingMessage object for the response, with all the headers, status codes, and other attached information instead.
It does not affect the network request being made.

Related

HTTP response body for server command request

I am solving one question with my team about REST API specification.
We have a case where in some requests we are sending only some particular command via HTTP request for example : We are using POST (now considering PATCH request) to endpoint : /server/startSomeOperation . Backend developers told us that this request is only telling hardware to start some functionality on backend that affects the measurement of the user but it really has nothing to return. My question is :
Should we (according to some REST API specification) always return body of such a request when we know that no additional returned data will be needed except HTTP status code? Until now we were strictly following the rule that every request needs to have some sort of body returned but until now every body response even when it was a command to a server made sense.
Should we (according to some REST API specification) always return body of such a request when we know that no additional returned data will be needed except HTTP status code?
No - it is perfectly fine to send a 204 No Content or a 205 Reset Content when the semantics of your response is well aligned with the standard meaning of those codes.
You also have the option of declaring that the "representation of the status of, or results obtained from, the action" is zero bytes long, in which case you could use a 200 OK with a Content-Length header.

REST API Response body same but response size different

We have a RESTful API built using ASP.NET Web API, and it is hosted as a Azure cloud service.
Recently we had to fix the performance (response time) of an endpoint owing to which we made a few changes. The API request-response needed to remain unchanged.
Thus to test that the changes we made didn't alter the response, we benchmarked the responses by capturing it for different users. We captured the following -
Response times (Postman display)
Response size (Postman display)
Response body
Now that we are testing, oddly we see that although the response body is an exact match (done using file compare) the response sizes are order of magnitude different. For instance what was 562.37KB before is now 52.33KB. In fact we had benchmarked 30 users and all the response sizes reduced by one order. But for all the response body is exactly the same.
What could be the possible reason? Is there anything we are missing?
Size is just the response size when it will be saved inside the memory. This response size is the size of complete response and headers and cookies and everything that has been sent along with the response.
NOTE: The response size that is shown in the Postman is approximate response size and not the exact size.
For details you may refer to
https://www.toolsqa.com/postman/response-in-postman/
https://github.com/postmanlabs/postman-app-support/issues/156
Secondly, it is important to know the difference as detailed in link size and content :
Chrome Dev Tools - "Size" vs "Content"
For easy access, a snapshot of the answer is below:
"Size" is the number of bytes on the wire, and "content" is the actual size of the resource. A number of things can make them different, including:
Being served from cache (small or 0 "size")
Response headers, including cookies (larger "size" than "content")
Redirects or authentication requests
gzip compression (smaller "size" than "content", usually

HTTP status code to return by a REST API when image size exceeds limit

A POST to a specific end point allows to upload an image except if the image is too large, so I want to return the appropiate http status code response in that case.
A http status code 400 response it does not seem to fit well in this case.
400 Bad Request: "The server cannot or will not process the request due
to something that is perceived to be a client error (e.g., malformed
request syntax, invalid request message framing, or deceptive request
routing).
I think that the image being too large it does not imply that the request is malformed or syntactically incorrect.
Any suggestions?
This seems like it would be an ideal candidate for 413 Payload Too Large. From Section 6.5.11 of RFC 7231:
The 413 (Payload Too Large) status code indicates that the server is
refusing to process a request because the request payload is larger
than the server is willing or able to process.
You can use 420 or even 422, but I would avoid that until you have really good reason to have separate code for it. Usually is better to keep number of different status codes rather small. Check top 10 on that list: http://www.restapitutorial.com/httpstatuscodes.html
You should avoid using more than 10 codes, because your API will become too complex.
So my answer is: use 400 with proper error message returned to the client like: "Image too large, you can upload files up to XX MB"

What is the benefit of passing HTTP status with API responses?

With your error responses you need to pass some attribute that indicates the error type anyway. So that makes HTTP status code redundant or sometimes even inaccurate.
Any real benefit here, and why it shouldn't just be 200?
Assuming we're talking about a RESTful API:
Because if you return an error message with a HTTP Status Code of 200, the client will assume his request was successful, and maybe not read the response body at all.
If the status code is 200, the client will not be looking for an error message in the response body.
An HTTP status code is not redundant. Pretty much in the same way a book's index is not redundant. First you look at the index for a topic you want to read on, and then you flip to that page and read more on that topic. Similarly, the client first reads the status code to know what happened, and then reads the response body to find out more about it.

how to get request header and response header by webkitGTK

I want to get the request header before the webkitGTK begin to send request and load the page. Then I want to get the response headers. However, I don't find such API in webkitGTK.
In Webkit1 the signal you want is WebKitWebView::resource-request-starting: The request argument contains a WebKitNetworkRequest which has a SoupMessage which will allow you to modify the request headers. The same signal has resource argument which has a response-received which in turn will contain a WebKitNetworkResponse. The SoupMessage of that response will have the response headers you want.
In Webkit2GTK the WebKitWebView::resource-load-started should be useful for at least monitoring: request and resource arguments work almost like the WebKit1 versions. The bit that I'm not 100% sure about is whether you can modify the request headers here -- or if you have to implement a WebExtension and use the WebKitWebPage::send-request signal.