Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created January 18, 2016 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/38bba130c7a5ed3121b7 to your computer and use it in GitHub Desktop.
Save podhmo/38bba130c7a5ed3121b7 to your computer and use it in GitHub Desktop.

👻

angular is ommting Content-Type header if request data is undefined.

server

pyhthon server.py

sample

# http is httpie
$ http http://localhost:8000 "Content-Type:application/json" | grep CONTENT
  "CONTENT_LENGTH": "",
  "CONTENT_TYPE": "application/json",
$ http http://localhost:8000 "Content-Type:text/html" | grep CONTENT
  "CONTENT_LENGTH": "",
  "CONTENT_TYPE": "text/html",
$ http http://localhost:8000 | grep CONTENT
  "CONTENT_LENGTH": "",
  "CONTENT_TYPE": "text/plain",

from angular

node app.js
input: {}
Content type: text/plain
input: {"headers":{"Content-Type":"application/json"}}
Content type: text/plain
input: {"headers":{"Content-Type":"application/json"}, "data":""}
Content type: application/json
require('../setup')(function(angular){
'use strict';
function callAPI($http){
var data = {
method: "GET",
url: "http://localhost:8000",
};
return function(opts){
console.log("input: %s", JSON.stringify(opts));
return $http(Object.assign({}, data, opts))
.then(function(response){
console.log("Content type: %s", response.data.CONTENT_TYPE);
}).catch(function(err){
console.log("!");
console.log(err);
});
};
}
callAPI.$inject = ["$http"];
angular.module("app", [])
.factory("callAPI", callAPI);
var inj = angular.injector(["app", "ng"]);
var api = inj.get("callAPI");
api({}).then(function(){
return api({headers: {"Content-Type": "application/json"}});
}).then(function(){
return api({headers: {"Content-Type": "application/json"}, data: ""});
});
});
# -*- coding:utf-8 -*-
import logging
import json
logger = logging.getLogger(__name__)
from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server
def simple_app(environ, start_response):
setup_testing_defaults(environ)
status = '200 OK'
headers = [('Content-type', 'text/json; charset=utf-8')]
start_response(status, headers)
data = {k: v for k, v in environ.items() if isinstance(v, (int, float, str, bool))}
ret = bytes(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8")
return [ret]
httpd = make_server('', 8000, simple_app)
print("Serving on port 8000...")
httpd.serve_forever()
'use strict';
var benv = require('benv');
function setup(cb){
benv.setup(function(){
global.Node = window.Node;
benv.expose({
angular: benv.require(require.resolve("angular/angular"), "angular")
});
cb(window.angular);
});
}
module.exports = setup;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment