flutterdartabapcsrf-token

x-csrf-Token Flutter error from the API written in ABAP?


I'm trying to make http post in flutter using API written through SAP ABAP. I am using x-csrf-Token and when I debug the data I sent, I get a 403 error because the wrong token is received. By fetching, I directly retrieve the x-csrf-Token value and send the value I retrieved. However, it gives a 403 error. How can I solve this?

  Future<void> postData() async {
    final Map<String, dynamic> data = {
      "Bukrs": "ABC",
      "Hbkid": "12345",
      "Kunnr": "67890",
      "Name1": "Örnek İsim",
      "Waers": "USD",
      "Dlimt": "10000",
      "Rlimt": "5000",
      "Klimt": "7500",
      "Blimt": "3000"
    };
    print( jsonEncode(data));
    String csrfToken ='';
    String cookie ='';
    String basicAuth = 'Basic ' + base64Encode(utf8.encode('$username:$password'));
    final String apiUrl2 =
        "url2)?\$format=json";
    try {
      final response = await http.get(
        Uri.parse(apiUrl2),
        headers: <String, String>{
          'Authorization': basicAuth,
          'x-csrf-token': 'fetch',
        },
      );
      if (response.statusCode == 200) {
        print(json.decode(response.body));
        csrfToken = response.headers['x-csrf-token']!;
        cookie = response.headers['set-cookie']!;
        print("x-csrf-token: $csrfToken");
        print("\n *----------------------cookie: $cookie");
      } else {
        print("HATAAAA: ${response.statusCode}");
      }
    } catch (e) {
      print("Hata oluştu: $e");
    }

    String basicAuth2 = 'Basic ' + base64Encode(utf8.encode('$username:$password'));
    final String apiUrl =
        "url";
    print(csrfToken);
    try {
      final response = await http.post(
        Uri.parse(apiUrl),
        headers:{
          'Authorization': basicAuth2,
          'x-csrf-token': csrfToken,
          'Content-type': 'application/json',
          'Cookie': cookie,
        },
        body: jsonEncode(data),
      );
      print('Gönderilen Headerlar: ${response.request!.headers}');
      if (response.statusCode == 200) {
        print("Veri başarıyla gönderildi");
      } else {
        print("Veri gönderme başarısız: ${response.statusCode}");
      }
    } catch (e) {
      print("Hata oluştu: $e");
    }
  }

Postman header: enter image description here


Solution

  • I solved the problem. I received the cookies I received when purchasing tokens. I sent it in the post process.