14

I am trying to send a Json over HTTP post to update a record in my database. I've connected to the server but I'm getting a 415 "Unsupported Media Type" error when I run the request.

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
return new MaterialApp(
  home: new MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
String url = 'http://<Hostname>: 
<Port>/jderest/orchestrator/JDE_ORCH_Sample_UpdateMeterReadings_Generic';


Future<String> makeRequest() async {
var response = await http
    .post(Uri.encodeFull(url), body: json.encode({
  "NewHourMeterReading": "650",
  "EquipmentNumber": "34665",
  "NewFuelMeterReading": "650"
}), headers: {"Accept": "application/json"});

print(response.body);

}

@override
Widget build(BuildContext context) {
return new Scaffold(
    body: new Center(
        child: new RaisedButton(
          child: new Text('Make Request'),
          onPressed: makeRequest,
        )));
}
}

Can someone please let me know how to get past this error?

the error that I am facing is this.

I/flutter ( 5881): Unsupported Media Type

Screenshot of Response Headers/Status Code/Body

enter image description here

Sorry for the messy code, it didn't copy paste over every well.

3
  • Please show a screenshot with the response (Headers and Body) of your request. You can do it using PostMan or something like this Commented Nov 20, 2018 at 7:55
  • I added the header/statuscode/body responses screenshot to the question. Commented Nov 20, 2018 at 8:06
  • are you posting text/html content? Commented Nov 20, 2018 at 8:13

3 Answers 3

18

You'll have to add the content-type to your header, setting its value to application/json.

By specifying Accept you're saying that your client is able to understand that response type, not that your request content is of the JSON type.

Basically you're saying "hey there, I'm able to understand JSON, so you can send it to me and I'll be fine with it" but you're not saying "hey I'm going to send you a JSON, be prepared for it!"

For a better understanding, you can find more about Accept here and Content-type here.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to hear that :) Happy coding!
11

You are using incomplete headers for sending the json payload. That is why the server is not accepting you request.

Use the following headers instead:

headers: {
        "content-type" : "application/json",
        "accept" : "application/json",
      },

Comments

8

Send Json and Accept Json using:-

    Future<String> addData(Map<String, dynamic> request) async {
    final url = 'http_url';
    try {
      final response = await http.post(url,
          headers: {
        "content-type" : "application/json",
        "accept" : "application/json",
      },
      body: jsonEncode(request));
      final responseData = jsonDecode(response.body) as Map<String,dynamic>;
      return responseData['message'];
    } catch (error) {
      throw error;
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.