Implementing If-Match HTTP header in Express.JS - node.js

Context: I am trying to prevent data overwriting due to concurrent updates by multiple users on the same database entity.
Question: How can I implement If-Match HTTP header in POST calls to respond with HTTP status 412 (Precondition Failed) when the DB has already been modified by another user?
My intended approach:
When a POST call is made to POST /user/123, I want to compare the ETag available in If-Match header of the POST call with the ETag present in the response header of the endpoint GET /user/123. To accomplish this, I have to invoke the GET route from the POST route, to extract the ETag. Is this even possible?

You can invoke GET from the POST route using res.redirect('/getroutename')
This way you can compare the ETag received from the get call, and achieve your intended approach.

Related

How to send get request with a body?

I am trying to send a get request to my API to get a list of users. But I need there is an exclude list that the response must exclude. How can I send this exclude list in my GET request?
You can send a body with the request. Query parameters is probably the best way to do it though. The folks at Elastic.co say:
The truth is that RFC 7231—the RFC that deals with HTTP semantics and
content—​does not define what should happen to a GET request with a
body! As a result, some HTTP servers allow it, and some—​especially
caching proxies—​don’t.
The authors of Elasticsearch prefer using GET for a search request
because they feel that it describes the action—​retrieving
information—​better than the POST verb. However, because GET with a
request body is not universally supported, the search API also accepts
POST requests:
You cannot send a request body when making a GET request. However, you can add it as a query parameter. Alternatively, you can make a POST request.

QR code best approach for POST request from REST API

I'm setting up a website that will be mobile focused and one of the features I wan't to implement is users to be able to log an entry by just scanning a QR code.
For what I read is not really possible to make a POST request directly from a QR code, so I was thinking in two different options:
1. Make a GET request and then redirect that inside my server to a POST route in my routes.
So the URL would be something like https://example.com/user/resources/someresourceid123/logs/new and then this would create a POST request to https://example.com/user/resources/someresourceid123/logs/ and create the new entry to then send a response to the user but I'm not really sure this is the best approach or if it's possible at all.
My POST request only requires the resourceid which I should be able to get from req.params and the userid which I get from my req.user.
2. Do my logic and log the entry to my DB using the GET request to https://example.com/user/resources/someresourceid123/logs/new.
This would mean that my controller for that request will do everything needed from the GET request without having to make an additional POST request afterwards. I should be able to get both the resourceid and userid from the req object but not sure if being a GET request limits what I can do with it.
If any of those are possible, which would be the best approach?
I'd propose to go with a second option simply for the sake of performance. But you need to make sure your requests are not cached by any proxy, which is usually the case with GET requests.

Request with RabbitMQ NodeJs

I'm pretty new here, so hope I can get some help with a basic doubt which I couldn't get around yet.
I'm using NodeJs and I have followed the Rabbiq GetStart and could understand the flow, however my doubt is with regards Http request.
What I need:
Manage http (POST, PUT, GET, DELETE) requests to another server.
What I was expecting:
RabitMQ manage the request QUEUE, so if some request fail it would retry again. When its successful, it would call another API on my end to flag the request was successfull.
What is my question:
I couldn't find any example which I would setup this request, providing the sender URL, METHOD and PAYLOAD and also the callback URL, METHOD, HEADERS, and PAYLOAD.
Is that something related to RabbitMQ or am I getting it wrong?

Accessing response headers from NodeJS/ExpressJS before a response is sent

Is there a way to see which response headers will be present on an HTTP response (and possibly set new HTTP headers)? It seems like once res.send() or res.json() is called, ExpressJS takes over, and there is no way to "intercept" the response before it is sent to the client. Note that I've seen this asked and answered for different platforms, its just not clear whether this is possible in NodeJS/ExpressJS.
I ended up asking this on the ExpressJS issues board. The answer from dougwilson:
For both use-cases you listed, you want to alter/inspect the response
headers. https://www.npmjs.org/package/on-headers is what you would
use.
Source: https://github.com/strongloop/express/issues/2327

ServiceStack - OnEndRequest capturing Response body

I have a RequestLog feature completely decoupled from the application logic.
I capture the request/response in a pre request filter. To acomplish this, I instantiate a request scope object that keeps the request context. And before everything gets disposed (at AppHost's OnEndRequest handler), I write to the db. One line per http req.
I'm able to access the response code, the path, the method, request body, request headers, etc.
However the response stream is not available as it was already disposed. What's the logic behind this? Is it something like, IIS writes the stream content to the wire, and releases the resource imediately? Is there any way I can capture the Response body at the OnEndRequest handler?
Thanks
No, ServiceStack doesn't buffer the Response stream it gets written directly to the ASP.NET response.
You can add a Global Response Filter or Custom ServiceRunner to capture the Services response if the request reaches that far, but the request can be short-circuited at anytime throughout the request pipeline by closing the response.
I had the same issue with capturing the response in ServiceRequestLogger. Read this post - it is solved in 4.0.39+ (currently pre-release)
ServiceStack response filter "short circuiting" causing problems

Resources