Nativescript: Http module does not accept URLs with spaces on iOS

Created on 7 Apr 2017  路  8Comments  路  Source: NativeScript/NativeScript

Did you verify this is a real problem by searching Stack Overflow and the other open issues in this repo?

Yes

Tell us about the problem

http package from tns-core-modules does not escape passed in URLs on iOS. This causes inconsistent behavior on Android and iOS: on Android passing in URLs with spaces works fine, on iOS http module returns "Unsupported URL" error.

Which platform(s) does your issue occur on?

iOS

Please provide the following version numbers that your issue occurs with:

  • CLI: 2.5.3
  • Cross-platform modules: 2.5.2
  • Runtime(s): 2.5.0
  • Plugin(s): -

Please tell us how to recreate the issue in as much detail as possible.

Sample project attached. See app/main-view-model.js for details.

Is there code involved? If so, please share the minimal amount of code needed to recreate the problem.

var http = require('http');
http.request({ method: 'GET', url: 'http://httpbin.org/cookies/set?TEST=0 1 2 3'}); // returns error on iOS, works fine on Android
android ios question

Most helpful comment

It would be quite handy to write that in the documentations. I struggled for something like 40 minutes trying to understand why some urls were fetched and some weren't in iOS (while in android it was working perfectly).

I mean, since it's a different behavior which is handled differently by the two operating systems apparently (since android resolves them properly), it would be fun to have an example or a reminder in the official documentation.

All 8 comments

try to put a .replace(/ /g, "%20") at the end of your url variable

Hi @otaran,
Thank you for the sample project.
I was able to reproduce this issue on Android as well as on iOS. The returned error on Android will be Code 505, which is related with the whitespaces between the values at the end of the URL. For further info, you could review this thread in StackOverflow.

In regard to that as a temporary solution, you could use the @Cae2 workaround and to replace the white spaces with "%20".

@Cae2, thanks for the workaround, I am using a similar approach (encodeURIComponent) in my app. I thought I should report this issue anyway because it looks like an inconsistency in Android vs iOS library behavior.

@tsonevn, thanks for checking this out! I am quite surprised that you observe an error on Android because it works fine for me on Android emulator (standard Android emulator running a Nexus_5_API_25 image, if that matters), i.e. the Android app requests an escaped URL http://httpbin.org/cookies/set?TEST=0%201%202%203.

Hi @otaran,
After a discussion with the NativeScirpt developer's team, we conclude that the URL representation should be handled by the app developer and there is no need to include additional functionality in the HTTP modules, which to encode the white spaces.
For this case, the solution with using replace method seems to be a perfect solution for handling this scenario.

Let me know, whether this is applicable for you.

Thanks for taking my request into consideration, @tsonevn! I will close this issue.

It would be quite handy to write that in the documentations. I struggled for something like 40 minutes trying to understand why some urls were fetched and some weren't in iOS (while in android it was working perfectly).

I mean, since it's a different behavior which is handled differently by the two operating systems apparently (since android resolves them properly), it would be fun to have an example or a reminder in the official documentation.

ERROR Error: Uncaught (in promise): Error: unsupported URL
file:///app/tns_modules/tns-core-modules/http/http-request/http-request.js:87:37 []
UIApplicationMain@[native code] []

`

url = url.replace(/ /g, "%20")

`
replacing spaces in url with %20 .works fine for me thats alot.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings