REST API best practice - where to place a unique request identifier
Asked Answered
S

1

20

When designing a REST API, what is the best practice for adding a unique request identifier when performing an http request?

I would normally add it in the headers as x-request-id, but today heard someone mention adding it in the url as a query string!

Also after doing some research, it seems like some people add it in the response body and send it as part of the payload!

Out of these three which would be the best application of a unique request identifier and why? What are the possible pros and cons of each approach?

Straus answered 16/7, 2018 at 13:40 Comment(2)
What do you need a request identifier for?Abut
To track a request through multiple requests in an internal micro-service architecture!Straus
A
14

HTTP doesn't include any request identifiers.

However, if you need one (for debugging or log enhancements, for example), you could use a header of your choice. Headers such as X-Request-Id, X-Correlation-Id and X-Trace-Id look fine for the scenario described in your comment: track a request through multiple requests in an internal micro-service architecture.

The value of such header could be an UUID.


This article will probably give you some insights. Also have a look at Zipkin.

Abut answered 16/7, 2018 at 13:57 Comment(4)
I know HTTP doesn't include any request identifiers. Previously I have used UUID to assign a value to my custom http request header. What about certain proxies stripping headers, which I found some of the proponents of using the payload, arguing was why they didn't use headers?Straus
@Straus Any request metadata should belong to the headers. Well, it's true that some proxies may strip non-standard headers out. But assuming that this header will be used in your microservices cluster (what we hope to be a controlled environment), I don't see major issues with that.Abut
@Abut I have a similar need, that is to manage a unique id to match each request that arrives at my API, in order to be able to create a chain (blockchain type) of all the calls to my service made from each customer. In addition, just to create the chain, at each call to any method of the API, I want to insert this unique id as a mandatory parameter, whose value is returned each time with the previous call to any method. Is there a pattern to achieve this?Darreldarrell
@Abut I've created a dedicated question. Please take a look at #68019199Darreldarrell

© 2022 - 2024 — McMap. All rights reserved.