0
A
回答
0
這是我嘗試將問題的第一部分,內容協商:
const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
//const dataAPI = require('../models/traffic');
router.get('/locations/:geohash/traffic/last-hour', (ctx, next) => {
// some code for validating geohash goes here ...
if (ctx.request.type=='application/vnd.api+json') {
//ctx.body = dataAPI.getTrafficData(ctx.params.geohash, 'hours', 1);
ctx.body = { status: "success" };
ctx.type = "application/vnd.api+json";
next();
}
else {
ctx.throw(406, 'unsupported content-type');
// actual error will be in JSON API 1.0 format
}
});
我得到的郵差狀態406 Not Acceptable
和unsupported content-type
當我提交值內容 - 在郵遞員中輸入任何不是application/vnd.api + json的東西。否則,我會在車站內找到200 OK
和{ "status": "success"
。
編輯
還沒有找到這更好的,但下面是一個快速和骯髒的方法來提取的x-api-key
值。它適用於我的目的:
var key = ctx.request.headers['x-api-key']
相關問題
- 1. 如何在c#中處理沒有http頭的SOAP/XML請求?
- 2. 如何在PHP請求中處理長頭部聲明?
- 3. 如何在nginx中處理「OPTIONS *」請求?
- 4. 如何在ColdFusion中處理請求
- 5. 如何在python中處理post請求
- 6. 如何在CherryPy中處理Web請求?
- 7. 如何在node.js中處理POST請求
- 8. 如何在AngularJS中處理CORS請求
- 9. SAML - 如何處理請求?
- 10. 如何處理HTTP請求
- 11. 如何獲取Google App Engine Webapp2請求處理程序中的HTTP標頭
- 12. 請求標頭和DOM中的欺騙請求標頭
- 13. 如何在SOAP請求中處理多個標記Body
- 14. Chaning與Koa.js的兩個HTTP請求
- 15. 如何在每個請求上設置多個請求標頭
- 16. 如何找出請求對象中的所有請求標頭?
- 17. 如何理解Chrome DevTools中的請求標頭?
- 18. 如何處理PHP中的請求
- 19. 如何處理ASP.NET中的* .txt請求
- 20. 如何處理Finatra中的請求?
- 21. 請求處理
- 22. 請求HTTP標頭
- 23. wsgi如何處理具有相同名稱的多個請求標頭?
- 24. 在Haskell處理/ Warp處理POST請求
- 25. 如何設置原始請求標頭
- 26. 如何設置curl的請求標頭?
- 27. 如何爲http請求添加標頭
- 28. 在java中設置請求標頭
- 29. 在ajax中設置請求標頭
- 30. 在硒中設置請求標頭
你試過什麼? – Evert
if(!ctx.accepts('application/vnd.api + json')){ctx.throw(406,'unsupported content-type'); } //但郵遞員沒有獲得狀態406。對於x-api-key找不到任何文檔 – Roobie
如果您需要幫助,請在您的問題中包含一個完整的,可重現的腳本! – Evert