2

I developed a mobile app that needs to access and update data on a server. I'd like to include e.g. the device ID and some token in each request.

I am including these in the body at the moment, so I have only POST requests, even when asking to read data from the server. However, a request to read data should be GET, but how do I include these pieces of information? Should I just add a body to a GET request? Should I rather add some headers? If so, can I just create any custom headers with any name? Thank you for your guidance.

user2923322
  • 1,072
  • 1
  • 11
  • 25
  • Search for the bearer authentication scheme. – MvdD Oct 19 '18 at 20:37
  • @MvdD When I search for bearer, it talks about authentication. However, I'm not sure I'm really authenticating, well kinda... The token I describe is a FCM (Firebase Cloud Messaging) registration token. It is not a JWT. When an android user logs in, I register the FCM token generated by the mobile device in the server's DB and subsequent calls including the token are authorized to call the server's API. So the token itself does not include identification as a JWT would. Is Bearer still relevant? And how would I then include the device ID? – user2923322 Oct 19 '18 at 20:51
  • 1
    While JWT is often used with the bearer authentication scheme, it's not strictly required. You could concatenate your FCM token and the device id like the basic authentication scheme does. It's really up to you how the credentials are formatted. – MvdD Oct 20 '18 at 21:12
  • @MvdD so I could have as Authorization Bearer: fcm_token + '@' + device_id. Well that answers the questions:) if you wanna write a short answer I can accept it. – user2923322 Oct 21 '18 at 16:35
  • Cool, I've converted my comments into an answer. Thanks. – MvdD Oct 22 '18 at 01:22

3 Answers3

2

Your FCM token and device id are really authentication credentials for the request. In HTTP, you typically use the Authorization header with a scheme to indicate to the service

In your case, you could use bearer tokens in the HTTP Authorization header. While bearer tokens are often used with JWT token, they are not required to be that specific format.

You could just concatenate the FCM token and the device id like the basic authentication scheme does.

BTW, it's not recommended to use a body on a GET request since some proxies may not retain that.

Community
  • 1
  • 1
MvdD
  • 22,082
  • 8
  • 65
  • 93
1

Well, REST is basically just a generalization of the concepts used already for years in the browser-based web. On applying these concepts consistently in your applications you'll gain freedom to evolve the server side while gaining robustness to changes on the clientside. However, in order to benefit from such strong properties a certain number of constraints need to be followed consequently like adhering to the rules of the underlying transport protocol or relying on HATEOAS to drive application state further. Any out-of-band information needed to interact with the service will lead to a coupling and therefore has the potential to either break clients or prevent servers from changing in future.

A common misconception in REST achitecture design is that URIs should be meaningful and express semantics to the client. However, in a REST architecture the URI is just a pointer to a resource which a client should never parse. The decision whether to invoke the URI should soly be based on the accompanying link relation name which may further be described in either the media-type or common standards. I.e. on a pageable collection link relation like prev, next, first or last may give a client the option to page through the collection. The actual structure of the URI is therefore not important to REST at all. Over-engineered URIs might further lead to typed resources. Therefore I don't like the term actually. How do non-restful-urls look like then?

While sending everything via POST requests is technically a valid option, it also has some drawbacks to consider though. IANA maintains a list of available HTTP methods you might use. Each method conveys different promisses and semantics. I.e. a client invoking a GET operation on a server should be safe to assume that invoking the resource does not cause any state changes (safe) and in case of network issues the request can be reissued again without any further considerations (idempotent). These are very important benefits to i.e. Web crawlers. Besides that intermediary nodes can determine based on the request method and the resulting response if the response can be cached or not. While this is not necessarily an issue in terms of decoupling clients from servers, it helps to take away unnecessary workload from the server itself, especially when resource state is rarly changing, improving the scalability of the whole system.

POST on the otherhand does not convey such properties. On sending a POST request for retrieving data the client can't be sure if the request actually lead to changes on the resources state or not. On a network issue the request might have reached the server and may have created a new resource though the response just got lost mid way which might keep the client in a state of uncertainty whether it simply can resend the request or not. Also, responses for POST operations are not cacheable by default, only after explicitely adding frehness information to it. A POST method invocation requests the target resource to process the provided representation accoding to the resources own semantics. As literally anything can be sent to the server it is of importance that the server teaches the client on how a request should look like. In HTML i.e. this is done via Web forms where a user can fill in data into certain input fields and then send the data to the server on clicking a submit button. The same concept could be applied for mobile or REST applications as well. Either reusing HTML forms or defining an own application/vnd.company-x.forms+json where the description of that media type is made public (or registered with IANA) can help you on this.

The actual question on where to include certain data is, unfortunately, to generic to give a short answer. It further depends whether data should be shareable or has some security related concerns. While parameters might be passed to the server via URL parameters (query, matrix, path) to a certain extent, it is probably not the best option in general eventhough query parameters are encrypted in SSL interactions. This option, though, is convenient if the URI should be pastable without losing information. This of course then shouldn't contain security related data then. Security related information should almost always be passed in HTTP headers or at least the actual payload itself.

Usually you shoud distinguish between content and meta-data describing the content. While the content should be the actual payload of the request/response, any meta-data describing the content should go inside the headers. Think of an image you want to transfer. As you don't want to mess with the bytes of the image you simply append the image name, the compression format and further properties describing how to convert the bytes back to an image representation within the headers. This discrimination works probably best for standardized representation formats as you need to be within the capabilities of the spec to guarantee interoperability. Though, even there things may start to get fuzzy. I.e in the area of EDI there exist a couple of well-defined standards like Edifact, Tradacoms, and so forth which can be used to exchange different message formats like invoices, orders, order responses, ... though different ERP systems speak different slangs and this is where things start to get complicated and messy.

If you are in control of your representation format as you probably did not standardize it or defined it only vaguely yet things might even be harder to determine whether to put it insight your document or append it via headers. Here it solely depends on your design. I have also seen representations that defined own header sections within the payload and therefore recreated a SOAP like envelop-header-body structure.

Roman Vottner
  • 12,213
  • 5
  • 46
  • 63
  • Thank you for that elaborate answer:) That was a long answer man!:D I eventually decided to concatenate the token and the device id and pass it through the header (Authorization Bearer) – user2923322 Oct 21 '18 at 16:46
1

About your question if you can create custom header for your requirement. My answer is YES.

As above was mentioned, you can use the standard Authorization header to send the token in each request . Other alternative is defining a custom header. However you will have to implement by server side a logic to support that custom header .

You can read more about it here