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?
REST API Best Practices: Always choose JSON One of the important best practices to follow is to always choose JSON. The key feature of JSON is that it is very easy to parse and supports most frameworks. JSON can be used by any programming language.
Each REST API resource can be accessed by using a Uniform Resource Identifier (URI). The URI must contain the correct connection information to successfully call the API. The connection information consists of the host name where the web management service is running, and the port number that the service is using.
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.
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