1

I would like to know if there is a way to format special characters when parsing JSON requests from Flutter. I have tested a http request as such:

void curlRequest() async
  {
    String urlRequest = "http://my.web.url/webservice/rest/server.php?wstoken=my_token&moodlewsrestformat=json&wsfunction=core_user_create_users&users[0][username]=test_user_4&users[0][firstname]=John&users[0][lastname]=Doe&users[0][email][email protected]&users[0][password]=Johns_Password";

    http.post(urlRequest, headers: {'Content-type': 'application/json; charset=utf-8'}).then((response){
        print("Response status: ${response.statusCode}");
        print("Response body: ${response.body}");
    });
  }

I get the following response:

http://my.web.url/webservice/rest/server.php?wstoken=my_token&moodlewsrestformat=json&wsfunction=core_user_create_users&users%5B0%5D%5Busername%5D=test_user_4&users%5B0%5D%5Bfirstname%5D=John&users%5B0%5D%5Blastname%5D=Doe&users%5B0%5D%5Bemail%[email protected]&users%5B0%5D%5Bpassword%5D=Johns_Password&i=1

The request was invalid due to special characters being used. How do I make a request so that the special characters are also handled properly?

7
  • Which API you are trying to call POST or GET? Commented Nov 23, 2020 at 12:42
  • It's the http package: pub.dev/packages/http Commented Nov 23, 2020 at 12:47
  • Yes I totally understand the package but you must have type of API which you are calling, I doubt you are trying wrong way to call POST API. Commented Nov 23, 2020 at 12:54
  • I'm sending requests to the Moodle API. I have tested the Moodle API and it works fine. Commented Nov 23, 2020 at 13:02
  • flutter.dev/docs/cookbook/networking/send-data maybe this can help also try raw string Commented Nov 23, 2020 at 13:16

2 Answers 2

2

In a http restful request, the http GET request must be url encoded, which means that most special characters must be encoded in a way understandable by a webserver.

As such, characters such as the plus sign (+) or the question mark (?) must be replaced by their URL encoded equivalents (%2b for the plus sign and %3F for a question mark for instance) and vice versa for decoding.

Several URL encoders can be found online such as http://www.url-encode-decode.com/

In Dart you can use Uri.encodeFull() to encode string and Uri.encodeComponent() to encode a string component

Similarly to decode you can use Uri.decodeFull() and others

read more about this here Uri class

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for pointing that out. I'm a bit new to network programming, so I thought that might have been the issue. Not sure why my request is not working from my Flutter app. If I run the same request in the browser it works as expected.
Cool no worries, if this answer helped you please Upvote this and accept this answer as well, I will be delighted by that @meeth23
Unfortunately, I require 15 points to upvote. Will do so when I can actually accept my own answer.
0

Switching to a better web hosting service resolved my issue. I was using AeonFree, which was a free web hosting service and had limited access for obvious reasons.

I also switched from the http package to using the dio package.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.