How to remove double quotes from the beginning and end of a Dart variable?

Asked

Viewed 115 times

2

Speak devs, all right?

I am unable to remove double quotes from the beginning and end of a Dart variable.

I post to my Web API and receive a JWT Object. I only need the value of the object, without the quotation marks at the beginning and end.

I tried to set objJWT[1:-1] but it didn’t work.

var response = await http.post(url, headers: header, body: _body);

    print('Responde body ${response.body}');

    var objJWT = response.body;
    objJWT = objJWT[1:-1]; 

Example of what ${answers.body} returns to me:

"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MTQyNzkwOTYsImV4cCI6MTYxNDI3OTEyNn0.si7GYefm0Iism09T91u3i1Kzo5cAYFJfdx-mbrv39Ys"

I need objJWT to stay this way:

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MTQyNzkwOTYsImV4cCI6MTYxNDI3OTEyNn0.si7GYefm0Iism09T91u3i1Kzo5cAYFJfdx-mbrv39Ys

1 answer

3


  • Remove first and last character from a string:

If you need to remove the first and last characters from a String, you can use something like:

 String A = "123456789";
 String B = A.substring(1, A.length-1);
 print(B);  //imprime 2345678

There are several ways to solve, but another approach could be to use the method replaceAll to replace all double quotes with empty ones. (Since you are sure that in the middle of your string there will be no other double quotes, for example.)


  • About getting the value of the object:

A JWT object consists of 3 strings encoded in Base64, joined by a point ".".

The three parts are:

  • Header ()
  • Cargo/Object (Payload)
  • Subscription (Signature)

More about JWT can be read here (in English).

This division can be seen in your return body example:

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MTQxOTI2MTcsImV4cCI6MTYxNDE5MjY0N30.xXyluFZo2DsHbdrW6R87F5Je6m64fVetW2HjqvjXh8c
|---------Cabeçalho ----------------|----------------Carga--------------------------|-----Assinatura---------------------------|

I believe you are interested in the content, so you need to check the central Payload component (Payload).

To do this, you can use a ready package (a quick search and found that). I imagine there are others who propose to this function. It would make your job easier, because using it you could:

import 'package:jwt_decode/jwt_decode.dart';

Map<String, dynamic> _mapResponse = Jwt.parseJwt(response.body);

Another alternative would be to implement your own decoder, which would not be too difficult since the library dart:convert already has methods for Base64.

First it would be necessary to divide your body by the points and get only the part that matters (payload):

String payload = response.body.split('.')[1];

A String in Base64 to be decoded needs to have a multiple character number of 4. If it does not have it, we need to complete it with the same character ("="). This can be done by code:

switch (payload.length % 4) {
    case 0:
      break;
    case 2:
      payload += '==';
      break;
    case 3:
      payload += '=';
      break;
    default:
      throw Exception('String não confere com uma codificada em base64.');
  }

Then just use the native function of dart:convertand decode:

import 'dart:convert';
Codec<String, String> stringToBase64Url = utf8.fuse(base64Url);
String jsonDecodificado = stringToBase64Url.decode(payload); 

At that time, the variable jsonDecodificado will contain the json you want. In your example:

{"iat":1614192617,"exp":1614192647}

Finally, just follow up with creating Dart’s common map:

Map _mapResponse = json.decode(payload);

What should get the desired map.

Remarks:

  • The functions I’ve done here are just an example, so you don’t have all the treatments. For example, you can check if the JWT token has 2 "." characters before you split.
  • I assumed that you are only interested in payload. The same process can be applied in the other fields.
  • Thank you very much! I already left work, but in the morning I will definitely test. Thanks in advance!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.