Flynn
Flynn

Reputation: 143

Flutter - Send Json over HTTP Post

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.

Upvotes: 14

Views: 17270

Answers (3)

DeePanShu
DeePanShu

Reputation: 1326

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;
    }
  }

Upvotes: 8

Harsh Bhikadia
Harsh Bhikadia

Reputation: 10885

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",
      },

Upvotes: 11

magicleon94
magicleon94

Reputation: 5192

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.

Upvotes: 18

Related Questions