Home > OS >  how to convert python requests http to flutter?
how to convert python requests http to flutter?

Time:06-28

enter image description here In python, this code is running and get response like screen shot.

I need this code in flutter. so i write the code like this.

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart';
import 'package:dio/dio.dart';
void main() async {
  var url = 'https://www.lottecinema.co.kr/LCWS/Ticketing/TicketingData.aspx';

  var dic = {
    "paramList": {
      "MethodName": "GetPlaySequence",
      "channelType": "HO",
      "osType": "W",
      "osVersion":
          "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
      "playDate": "2022-06-29",
      "cinemaID": "1|0001|1013",
      "representationMovieCode": ""
    }
  };

  var req = await Dio().post(
    '$url',
    data: dic,
  );
  print(req);


  runApp(const MyApp());
}

But the result is enter image description here -> 'ParamList가 존재하지 않습니다' = 'ParamList does not exist.'

Another code is this.

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart';
import 'package:dio/dio.dart';

void main() async {
  var url = 'https://www.lottecinema.co.kr/LCWS/Ticketing/TicketingData.aspx';

  var dic = {
    "paramList": {
      "MethodName": "GetPlaySequence",
      "channelType": "HO",
      "osType": "W",
      "osVersion":
          "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
      "playDate": "2022-06-29",
      "cinemaID": "1|0001|1013",
      "representationMovieCode": ""
    }.toString()
  };

  var request = await http.MultipartRequest('Post', Uri.parse('$url'));
  request.fields.addAll(dic);

  http.StreamedResponse response = await request.send();
  var responseString = await response.stream.bytesToString();

  print("STATUS CODE : ${response.statusCode}");
  print("RESPONSE BODY : ${responseString}");

  runApp(const MyApp());
}

enter image description here

The paramList what i need and expected result are in this screenshot

enter image description here

enter image description here

how can i solve this problem?

CodePudding user response:

It seems that the api end point only accepts data in the multipart/formdata format.

import 'dart:covert';
...
  
String paramList = json.encode({
    "MethodName": "GetPlaySequence",
    "channelType": "HO",
    "osType": "W",
    "osVersion":
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
    "playDate": "2022-06-29",
    "cinemaID": "1|0001|1013",
    "representationMovieCode": ""
  });

FormData formData = FormData.fromMap({'ParamList': paramList});

var req = await Dio().post(
  '$url',
  data: formData,
);
  • Related