0

For various REST api endpoints, the user_id will reach the backend, needed for further processing and then, sent back as a response to the front end.

I have a feeling I can do this through the header instead of passing it as a path parameter each time, except I can't seem to find the relevant information yet.

At the moment I send the response as a ResponseEntity. I would like, if possible, to keep this option.

I am using Java and Spring Boot.

Art
  • 39
  • 1
  • 9
  • Does this answer your question? [How to get access to HTTP header information in Spring MVC REST controller?](https://stackoverflow.com/questions/19556039/how-to-get-access-to-http-header-information-in-spring-mvc-rest-controller) – Florian Cramer Jan 25 '23 at 09:13
  • Hi @FlorianCramer, not really, but thank you for the answer! – Art Jan 25 '23 at 09:25

2 Answers2

1

example based on

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

edited to add readign header from request

@RequestMapping("/handle")
public ResponseEntity<String> handle(HttpServletRequest httpRequest) {
  String userId= httpRequest.getHeader("user_id");
  HttpHeaders responseHeaders = new HttpHeaders();
  responseHeaders.set("user_id", userId);
  return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}
lukwas
  • 232
  • 1
  • 2
  • 11
  • Hi @lukwas, thanks for the reply. I see your answer is related to how to append it when sending it back. While this will definitely be helpful, first I need to understand how I can grab it from the header when request is coming in. So, I would assume some code is needed before the "URI location = ....; " you wrote. – Art Jan 25 '23 at 09:04
  • @Art location was copy from spring example, I edited answer to read header from request and add it to response – lukwas Jan 25 '23 at 09:23
  • 1
    @Art `getHeader(String name)` returns `String` , You can parse it to long using `Long.parseLong(...)` – lukwas Jan 25 '23 at 09:30
  • why did you use HttpServletRequest instead of RequestEntity as the parameter for handle? Can you please detail that a bit? – Art Jan 25 '23 at 09:33
  • 1
    @Art `httpRequest` is easier, You can use `RequestEntity request` and get header using `Long userId = Long.parseLong(request.getHeaders().getFirst("user_id"));` – lukwas Jan 25 '23 at 09:50
  • if you found my question interesting, please give it a thumbs up, it helps me and others that might be facing the same challenge. thanks! – Art Jan 25 '23 at 11:11
0

I have decided that the best approach for my scenario, where I only need to fetch the user id and then respond back with it, is to use the @RequestHeader("userId") Long userId annotation.

Let's have a look at how I had configured the enpoint initially:

@PostMapping(path = "/add-follower/{userIdForFollowing}/{currentUserId}")
public ResponseEntity<String> addFollower(@PathVariable ("userIdForFollowing") Long userIdForFollowing, @PathVariable Long currentUserId)
{
    Follow newFollow = followService.returnNewFollow(userIdForFollowing, currentUserId);
    
    newFollow = followService.saveFollowToDb(newFollow);

    return new ResponseEntity<>("Follow saved successfully", HttpStatus.OK);
}

Now, let's look at how I refactored the endpoint to fetch the id's from the header and return them in the response:

@PostMapping(path = "/add-follower")
public ResponseEntity<String> addFollower(@RequestHeader("userIdForFollowing") Long userIdForFollowing, @RequestHeader("currentUserId") Long currentUserId)
{

    Follow newFollow = followService.returnNewFollow(userIdForFollowing, currentUserId);
    newFollow = followService.saveFollowToDb(newFollow);

    //here I will add more code which should replace the String in the ResponseEntity.
    return new ResponseEntity<>("Follow saved successfully", HttpStatus.OK);
}
Art
  • 39
  • 1
  • 9