3

Angular v1.3.5

I'm trying to pass serialized data to my API. It requires the Content-Type header to be application/x-www-form-urlencoded; charset=UTF-8;

For POST, I've set this up as follows in my .run:

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8;";

This works great for POST. However, for PUT doing the same is completely ignored.

$http.defaults.headers.put["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8;";

I even tried putting this in the request. The Content-Type was still ignored.

$http({
  method : 'PUT',
  url : SMARTWORX_CONFIGS.APIURL + 'users/' + service.profile.id + '.json',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;'
  },
  data : {
    user : profile
  }
})

I've been forced to solve this by using an interceptor and adding the headers to the config object there. It's quite a hack, but it works.

Am I doing something wrong here?

1 Answer 1

3

After beating my head on the wall some more, I discovered the problem - of my own making.

My app is using tokens for authentication; so, I have an interceptor to inject the token into the headers when needed. I made a mistake with this.

It looked like :

config.headers = config.header || {};
config.headers['X-AUTH-TOKEN'] = result;

It SHOULD have been written like:

config.headers = config.headers || {};
config.headers['X-AUTH-TOKEN'] = result;

Basically, I was blowing out all previous headers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.