Search code examples
flutterdarthttppatch

Dart http patch always returns 500


The following dart code always returns a status code of 500. Do you know why?

          String answers = json.encode(answersJson);
          answers = base64.encode(utf8.encode(answers));
          String uri =
              '${baseUri}questionnaire_instances_Ex(${modifiedQuestionnaireBlocks[i]})';
          String body = '{"answer_json_base64": "$answers"}';
          //String etag = rawJson['value'][j]['@odata.etag'];
          var etag = 'W/"JzE5OzE2MDEyNzU0ODExMzM3NjA1NzgxOzAwOyc="';
          var patchAnswersResponse = await http.patch(Uri.parse(uri),
              headers: {
                'Authorization': 'Bearer $accessToken',
                'If-Match': etag,
              },
              body: body);
          if (patchAnswersResponse.statusCode == 200) {
            modifiedQuestionnaireBlocks.remove(modifiedQuestionnaireBlocks[i]);
          } else {
            //TODO: error handling
          }

Solution

  • I managed to solve the error:

    1: Headers needed 'Context-type' : 'application/json'
    2: body needs to be encoded in json.