cordova-plugin-advanced-http icon indicating copy to clipboard operation
cordova-plugin-advanced-http copied to clipboard

POSTing valid JSON entities beside objects

Open sneas opened this issue 4 years ago • 4 comments

Hi, @silkimen. Thanks a lot for your hard work on the plugin!

The question: using the plugin, is there any way to send a valid JSON entity, but not an object. For example a string, a number, or a boolean value?

An example of such a request with cURL:

curl --header "Content-Type: application/json" \
  --request POST \
  --data '"This is a valid HTTP entity"' \
  https://httpbin.org/post

Unfortunately, attempting to do that with sendRequest method leads to the below error:

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

Is there any other way to achieve the described? Or are there any plans to provide the described functionality some time in the future?

sneas avatar Apr 20 '20 16:04 sneas

Hi Dmitry, atm it's not possible using the json serializer, because the underlying native API does not support it. But people keep asking for it, so I'm planning to support this feature in a future version.

For time being you can use following workaround:

// use utf8 serializer
cordova.plugin.http.setDataSerializer('utf8');

// serialize your data yourself
const serializedData = JSON.stringify('This is a valid HTTP entity');

// set the content-type 'application/json'
const headers = { 'Content-Type': 'application/json' };

cordova.plugin.http.post('https://google.com/', serializedData, headers, success, fail);

silkimen avatar Apr 21 '20 17:04 silkimen

Hi Sefa, thank you for your reply. It might help me to deal with the issue.

sneas avatar Apr 22 '20 06:04 sneas

Hi @silkimen, when I try to call sendRequest with the help of @ionic-native/http like below

this.http.sendRequest('https://httpbin.org/post', {
  method: 'post',
  headers: {
    'Content-Type': 'application/json'
  },
  serializer: 'utf8',
  data: JSON.stringify("json string")
}).then(data => console.log(data));

I receive a TS error TS2322: Type 'string' is not assignable to type '{ [index: string]: any; }'. because according to @ionic-native/http, data param could only be an object.

However, when I suppress the error, the plugin works find and as you described in your previous comment:

this.http.sendRequest('https://httpbin.org/post', {
  method: 'post',
  headers: {
    'Content-Type': 'application/json'
  },
  serializer: 'utf8',
  // @ts-ignore
  data: JSON.stringify("json string")
}).then(data => console.log(data));

Is that a problem with @ionic-native/http typings? Or am I trying to utilize an undocumented feature of cordova-plugin-advanced-http which might be gone someday?

sneas avatar Apr 29 '20 12:04 sneas

That's an official feature. I think we'll need to adjust the typings by using generics, because type of data is determined by serializer. You'll find a list of allowed data types here: README.md#setdataserializer

silkimen avatar Apr 30 '20 16:04 silkimen