silkimen / cordova-plugin-advanced-http

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

How to call a POST method without the data attribute in options ? #381

Open Ramya-Kakarla opened 4 years ago

Ramya-Kakarla commented 4 years ago

In case of POST method cordova plugin always expects the data to be sent. How to handle if there is no data available? Tried the workarounds shared in the link below, but it doesn't seem to work in my case. https://github.com/silkimen/cordova-plugin-advanced-http/issues/324 https://github.com/silkimen/cordova-plugin-advanced-http/issues/177. Sample code snippet of what I'm trying:

cordova.plugin.http.setServerTrustMode("pinned", function () { cordova.plugin.http.setDataSerializer("utf8"); cordova.plugin.http.setHeader("", "accept", String("application/json, text/plain, /")); cordova.plugin.http.setHeader("", "Content-Type", String("application/json")); const options = { method: "POST", data: "null" }; cordova.plugin.http.sendRequest(sampleURL, options, function(response){ console.log("Success"); }, function(e){ console.log("Failed"); } );

});

This approach is giving error 415 - Unsupported Media Type. What should be modified in the above code?

silkimen commented 3 years ago

Please format your example code properly. Read the documentation #setheader carefully. Something is wrong in following lines:

// your code
cordova.plugin.http.setHeader("", "accept", String("application/json, text/plain, /"));
cordova.plugin.http.setHeader("", "Content-Type", String("application/json"));

// I think you want it to be
cordova.plugin.http.setHeader("Accept", "application/json, text/plain");
cordova.plugin.http.setHeader("Content-Type", "application/json");

Please use StackOverflow for this kind of questions.

vandres commented 3 years ago

@silkimen I think we get the same problem. When sending a POST request with an empty data property, the plugin complains that it is expecting Array or Object. Strangely, this only happen, when we make a production build. In a dev build, the error doesn't occur.

Code:

const request: HttpRequest<any>;

...

return this.http.sendRequest(request.url, {
    'POST',
    data: request.body,
    headers,
    'json'
});

Error:

Error: advanced-http: "data" option is configured to support only following data types: Array, Object

It doesn't matter, if data is an empty string, undefined or anything else than e.g. {}. Sending a pure {} as a fallback resolves it, but it would be more convenient, if we could leave data empty. Especially since we don't only use the "json" serializer.

kgillespieatmosphere commented 3 years ago

I gave up trying to use this plugin for my needs.

https://github.com/aporat/cordova-plugin-fetch

I can recommend this plugin. It behaves like standard fetch API which everything pretty much uses and supports. It provides the same capabilities in terms of being able to process requests in the background and across different CORs domains. Plus you can send POSTs with any data type you want.

silkimen commented 1 year ago

You will face this problem only in case when you are using the json serializer. The reason is that an empty body does not represent a valid JSON instance.