Search code examples
cordovacordova-plugins

How to set Content-Type header in cordovaHTTP plugin requests


I'm trying to use cordovaHTTP plugin and in my POST api calls, although I've set Content-Type header to application/json but I can see that content is sent in application/x-www-form-urlencoded format and no Content-Type header exist in headers of request.

usage sample:

cordovaHTTP.post("url/to/method",
 {
   "email": "[email protected]"
 },
 {
   "Content-Type": "application/json"
 },
 function (result) {
   alert('success');
 },
 function (error) {
   alert('error');
 });

in the server api I see:

RAW BODY
email=ali%40aliha.me

How can I set Content-Type header?

Note: This approach of setting headers, works properly for other headers like Authorization header.


Solution

  • This plugin doesn't support Content-Type header in it's post method yet.
    I used a forked repository from dear brendonparker instead which has a postJson method for sending post requests with content-type of application/json.