Reputation: 631
For some reason the request headers I am getting by using request.headers
is not valid JSON which means I seem to be unable to parse it. The headers I am getting are as follows:
{ host: 'localhost:3000',
'content-type': 'application/json',
origin: 'http://localhost:3000',
'accept-encoding': 'gzip, deflate',
cookie: '__utma=111872281.423941909.1445035542.1451387074.1451408560.20; __utmc=111872281; __utmz=111872281.1445035542.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __jlptid4483=5633968d7051f52c388b457d; _ga=GA1.1.423941909.1445035542; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%2214408339bad21514d3a8deab593087ef%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A116%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_10_5%29+AppleWebKit%2F601.3.9+%28KHTML%2C+like+Gecko%29+Version%2F9.0.2+Safari%2F601.3.9%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1451413507%3B%7De88c3d74bb7153f8ddfb600814f1f249; csrftoken=Cw6hhwpl4RWtvXqRfqmfeok6cE33cvT4; sessionid=8gvksyocsx4ffcsp22ucgkxf84l7y4tg; spu_box_8075=true; wp-settings-time-1=1446207423',
connection: 'keep-alive',
accept: 'application/json',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9',
referer: 'http://localhost:3000/documentation',
'content-length': '67',
'accept-language': 'en-us' }
This basically means I have no way of extracting the user agent which is what I am attempting to get.
If someone could either help me with parsing this or finding an easier way to get the user agent.
Upvotes: 2
Views: 404
Reputation: 461
The request headers in hapi are provided as a JS object, not as JSON. You don’t need to worry about the encoding/decoding for the header fields, hapi got you covered.
hapi lets you access the request headers in your route handler by using request.headers
. Further, you can access fields like this
request.headers.host
request.headers['user-agent']
(headers with a -
dash need quotes)If you need more details, you can read more in this tutorial on how to access request headers in hapi. This guide also contains a video describing the headers in hapi.
Hope that helps!
Upvotes: 2
Reputation: 42063
It's a JavaScript object, you can access the user agent this way:
request.headers['user-agent']
Upvotes: 1