wymsee / cordova-HTTP

Cordova / Phonegap plugin for communicating with HTTP servers. Allows for SSL pinning!
MIT License
371 stars 300 forks source link

Making POST request with non-json data #114

Closed drblmb closed 7 years ago

drblmb commented 8 years ago

I cannot figure out how to create a POST request where the data is not in JSON. For example, in the body of my POST request I want "termsaccept=true&userid=confusedUser"

Please give me an example of this. Thanks!

sckumar commented 7 years ago

@drblmb I didn't find any methods to append the query string in the request body. Instead I defined a method to convert querystring into json. This method doesn't parse properly if the querystring has =,& symbols.

queryStringToJSON = function(str){
 var pairs = str.split('&');
 var result = {};
 pairs.forEach(function (pair) {
 pair = pair.split('=');
 var name = pair[0]
 var value = ""; value = pair[1]
 if (name.length)
 if (result[name] !== undefined) { 
 if (!result[name].push) {
 result[name] = [result[name]];
 }
 result[name].push(value || '');
 } else {
 result[name] = value || '';
 } 
 });
 return (result);
 }

drblmb commented 7 years ago

I was able to find some code that implemented a cordovaHTTP.postJson method. I forked this project and got it working. https://github.com/serviewcare/cordova-HTTP.