简体   繁体   English

如何从 dart/flutter 将 int/integer 值发布为 json

[英]How to post int/integer value as json from dart/flutter

I am using asp.net core web API as back-end.我使用 asp.net 核心 web API 作为后端。 There is a method that accepts a single integer value.有一种方法可以接受单个 integer 值。

Method([FromBody] int value)

I want to post the integer value from dart/flutter.我想从 dart/flutter 发布 integer 值。 I tried the following with the dart http package.我用 dart http package 尝试了以下操作。

Http.post(url, body:0,headers:{"content-type":"application/json"}

Http.post(url, body:{0},headers:{"content-type":"application/json"}

Http.post(url, body:convert.jsonEncode(0),headers:{"content-type":"application/json"}

Http.post(url, body:convert.jsonEncode({0}),headers:{"content-type":"application/json"}

All my above tries failed with error "Invalid argument: invalid request body "0""我上面所有的尝试都失败了,错误“无效的参数:无效的请求正文“0””

You can try this code.你可以试试这段代码。

Http.post(url, body:{"id": "0"},headers:{"content-type":"application/json"}

Please refer my code请参考我的代码

  1. import进口

    import 'package:http/http.dart' as http;
  2. http request http 请求

    var client = new http.Client(); client.post(Uri.encodeFull("Your url"), body: { "param1": "value1", "param2": 11, // integer value type }).then((response) { client.close(); if (this.mounted && response.statusCode == 200) { //enter your code for change state } }).catchError((onError) { client.close(); print("Error: $onError"); });

I hope it will help you.我希望它会帮助你。

PS: PS:

var client = new http.Client();
var response = await client.post(Uri.encodeFull("Your Url"), body : "0", header : {/*Your headers*/"});

Can you try bellow one你可以试试下面的一个

var queryParameters = {
                'value': '0'
        };
        var uri =
                Uri.https('www.myurl.com', '/api/sub_api_part/', queryParameters); //replace between  part and your url
        var response = await http.get(uri, headers: {
                    HttpHeaders.contentTypeHeader: 'application/json'
        });

I had the same problem when trying to send an HTTP request to an API that has an integer as one of its arguments(age).尝试将 HTTP 请求发送到具有 integer 作为其参数(年龄)之一的 API 时,我遇到了同样的问题。 Dart wanted me to convert the int into a string and the API was not accepting the int as a string. Dart 希望我将 int 转换为字符串,而 API 不接受 int 作为字符串。 Hence I was getting the same error and ended up in this question.因此,我遇到了同样的错误并最终出现了这个问题。

My solution was to store the input in a map, add a header {"content-type":"application/json"} and pass the map in the body arguement.我的解决方案是将输入存储在 map 中,添加 header {"content-type":"application/json"} 并在正文中传递 map。

import 'dart:convert';
import 'package:http/http.dart' as http;


Future<String> register_user() async {
  var req_body = new Map();

  req_body['username'] = 'John Doe';
  req_body['age'] = 20; /* The integer */

  final response = await http.post(
        'http://127.0.0.1:8081/user/register',
         headers: {'Content-Type': 'application/json'},
         body: jsonEncode(req_body));


  if (response.statusCode == 200) {
    var object = json.decode(response.body);

    return object.toString();
  } else if (response.statusCode == 422) {
    return 'Error';
  } else {
    return 'Can not connect to server';
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM