'How to send body as a raw in flutter

I can try on swagger or postman, that is working but i cannot send request in flutter. This is the output of Postman =>

--header 'Content-Type: application/json' \
--data-raw '"12312412543412"'

This is my code api part in flutter =>

Future loginComplete(id, token) async {
    final response = await dio.post('${wp.LoginCompletePage}userId=$id',
      data: "$token",
      options: Options(headers: {
            HttpHeaders.contentTypeHeader:"application/json",
            HttpHeaders.acceptHeader:"*/*"
      }));
    debugPrint(response.toString());
    if(response.statusCode != 200) {
      debugPrint(response.statusCode.toString());
    }
    return response;

Could anyone help me?



Solution 1:[1]

I don't know how you got the token but i think you need add this.

final data = json.encode({"token": token});

this works for me.


 Future loginComplete(id, token) async {
    
  final data = json.encode({"token": token});
    
   final response = await dio.post('${wp.LoginCompletePage}userId=$id',
      data: data,
      options: Options(headers: {
            HttpHeaders.contentTypeHeader:"application/json",
            HttpHeaders.acceptHeader:"*/*"
      }));
    debugPrint(response.toString());
    if(response.statusCode != 200) {
      debugPrint(response.statusCode.toString());
    }
    return response;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samuel Angel Dzib Sosa