• Post Reply Bookmark Topic Watch Topic
  • New Topic

Appending n headers to a response  RSS feed

 
Hans Hovan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if this is the correct forum, but here goes:

I'm creating a response with code like this:



That all works fine.

Now, what I'd like to do is add response headers. I could do something like this:



But I'll have a variable amount of headers stored in a map. My question then is what would be the best way to add n headers to my response? I have to pretty much work within the confines of the code that I've shown (the map of all the required headers is available to me wherever I need it). I think I'd run the map with a foreach loop appending as I go but I can't find a code example of how to do the actual appending. Not really sure how to approach this. Any help appreciated. Thanks!
 
Ron McLeod
Saloon Keeper
Posts: 1562
222
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your headers are in a map, you could use a ResponseBuilder, iterate through the map entries, and build them in to your response header-by-header.

 
Hans Hovan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the idea Ron.
I didn't even know a 'ResponseBuilder' existed. That is exactly the type of thing I was looking for. Thanks.
I'll try implementing it and see how it goes. I'll report back if any issues. Thanks again!
 
Hans Hovan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:If your headers are in a map, you could use a ResponseBuilder, iterate through the map entries, and build them in to your response header-by-header.




I tried to implement this and it works great for a GET but fails for POST. Any idea why this might be? Both GET and POST are passing in the same values.

When using Postman this code returns the anticipated response if it was from either a GET or a POST (without the response headers being attached)



I then implemented the changes you suggested and came up with this code:



This code appends all the headers and returns the response as expected when a GET request is made. But when a POST is made Postman "could not get any response".
I'll try running this through the debugger and playing around with it some more, but is there something obvious I am missing? Or does RequestBuilder not work with POST requests?

Thanks again for the help.
 
Ron McLeod
Saloon Keeper
Posts: 1562
222
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResponseBuilder can be used with any HTTP method.

If the server is not responding to the request, then I would look for error on the server side - check the logs.  It might be that the server doesn't allow you to include a payload in response to a POST and is throwing an exception.  If the POST is used to create a resource, then the response from the server would normally include a Location header with a URI for the newly created resource.

For example:
 
Hans Hovan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for the help Ron. I think I might have discovered the issue.

I was collecting all my headers in a map like this:



I tried printing out all of those headers as I added them to the response headers like you suggested.

When I made a POST in Postman, this was the list that was printed out in the debugger (as example):
0 Cache-Control no-cache
1 Accept */*
2 User-Agent PostmanRuntime/3.0.11-hotfix.2
3 Connection keep-alive
4 Postman-Token 804b859e-92a4-4703-a81b-aeb1d7f5d8d4
5 Host localhost:8080
6 id LBM1234
7 Accept-Encoding gzip, deflate
8 Content-Length 7
9 Content-Type application/x-www-form-urlencoded

I then manually created a test map, only adding a few elements at a time and then appending them as response headers to see if that would work. It did at first. I noticed that even though I wasn't adding a 'Content-Length' response header Postman was still displaying one that was ~250 length. I manually added a few more response headers and when I tried to add 'Content-Length' I was able to reproduce the error.

So it seems that Postman has a response header called 'Content-Length' and it was conflicting when I tried to add another response header called 'Content-Length' (that was being generated from the request headers).

So as long as I have something like this:



It seems to work OK and return all of the other headers. (It does also seem to overwrite the 'Content-Type' response header that I add with its own, the response type that I have set in the code json, but it doesn't cause a "could not get response" error).

So I think this solves my problem. Thanks for helping me think through it and for your suggestions Ron.
 
Ron McLeod
Saloon Keeper
Posts: 1562
222
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your welcome - glad you giot things sorted-out. 

You probably don't want to blindly copy-over all the headers from the request in to the response.  Many of the headers used in a request have no meaning or may even be be invalid in a response.  Also, as you found, you might end-up unintentionally over-writing headers added by the your framework or helpers with incorrect one's from the original request.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!