nativescript-community / https

Secure HTTP client with SSL pinning for Nativescript - iOS/Android
https://nativescript-community.github.io/https/
Other
51 stars 42 forks source link

body null with Content-type application/json; charset=utf-8 #54

Closed kefahB closed 4 years ago

kefahB commented 4 years ago

Hi @EddyVerbruggen ,

Just a little report about an issu with this plugin.

When I send the a request with the content-type: application/json; charset=utf-8 I receive an empty body on the server side ; The issu come from the charset=utf-8 .. declaring the content-type without it solve the issu .. but I think is preferable to keep the charset.

kefahB commented 4 years ago

It come from IOS: https.ios

Android: https.android

// IOS
let contentType = opts.headers['Content-Type'].substring(0, 16);

if (opts.headers && contentType === 'application/json') {
        manager.requestSerializer = AFJSONRequestSerializer.serializer();
        manager.responseSerializer = 
       AFJSONResponseSerializer.serializerWithReadingOptions(NSJSONReadingOptions.AllowFragments);
}

// Android
// For android I do not testing yet but from this line I think there is no problem
let type = opts.headers && opts.headers['Content-Type'] ? <string>opts.headers['Content-Type'] : 'application/json';
panagulis72 commented 4 years ago

@kefahB Although I have omitted to write 'charset = utf-8', can be my problem (https://github.com/EddyVerbruggen/nativescript-https/issues/59) related to what you have revealed?

kefahB commented 4 years ago

Hi @panagulis72 ,

Yes it can be the problem because like you see if you set the utf-8 the data wil not be serialized by the plugin .. if you want to be sure try to modify the https.ios.js on the plugin directly and change the code by the flowing :


var contentType = opts.headers['Content-Type'].substring(0, 16);

if (opts.headers && contentType === 'application/json') {
        manager.requestSerializer = AFJSONRequestSerializer.serializer();
        manager.responseSerializer = 
       AFJSONResponseSerializer.serializerWithReadingOptions(NSJSONReadingOptions.AllowFragments);
}```
I will do a PR about that and we will se what @EddyVerbruggen say about that
panagulis72 commented 4 years ago

Hi @kefahB , thanks for the quick feedback! I'm using android so I'm looking at https.android.js file. Debugging it, I saw it enters this condition:

if ((['GET', 'HEAD'].indexOf(opts.method) !== -1) || (opts.method === 'DELETE' && !types_1.isDefined(opts.body))) {
  request_1[methods[opts.method]]();
}

so it skips the type in the else condition (var type = opts.headers['Content-Type'] || 'application/json';) and it doesn't add parameters

kefahB commented 4 years ago

I dont now about android yet .. I facing this problem with IOS only .. I think there is no problem with android related with Content-type because it detect if content-type is set and get it as it :

// Android
// For android I do not testing yet but from this line I think there is no problem
let type = opts.headers && opts.headers['Content-Type'] ? <string>opts.headers['Content-Type'] : 'application/json';
panagulis72 commented 4 years ago

If I set the header application/json first, and then application/json;charset=utf-8 but is the same on android so I guess it works; the problem is that (trying with both Content-types) the params are not sent with GET Api call