pschlan / cron-job.org

cron-job.org Open Source project
GNU General Public License v2.0
1.44k stars 251 forks source link

Api error using Flutter-Dart #178

Closed jabirmayar closed 1 year ago

jabirmayar commented 1 year ago

Hey, So i wanted to use api. i tested it with python and it works. But in flutter it is giving me 400 error Python code:

import json
import requests

ENDPOINT = 'https://api.cron-job.org'

headers = {
    'Authorization': 'Bearer apikey',
    'Content-Type': 'application/json'
}
payload = {
    'job': {
        'enabled': True,
        'schedule': {
        "timezone": "UTC",
        "hours": [-1],
        "mdays": [-1],
        "minutes": [-1],
        "months": [-1],
        "wdays": [-1]
      }
    }
}

result = requests.patch(ENDPOINT + '/jobs/11111', headers=headers, data=json.dumps(payload))
print(result.json())

Dart Code:

Future<void> updateCronTime(JobSchedule newTime) async {
  final headers = {
    'Authorization': 'Bearer apikey',
    'Content-Type': 'application/json'
  };

  final payload = {
    'job': {'enabled': true, 'schedule': newTime.toJson()}
  };

  final response = await http.patch(
      Uri.parse('https://api.cron-job.org/jobs/11111'),
      headers: headers,
      body: jsonEncode(payload));

  if (response.statusCode != 200) {
    print(response.statusCode);
    throw Exception('Failed to update cron time');
  }
}

where JobSchedule is

`class JobSchedule {
  List<int> hours = [-1];
  List<int> mdays = [-1];
  List<int> minutes = [-1];
  List<int> months = [-1];
  List<int> wdays = [-1];
  String timezone;

  JobSchedule({required this.timezone});

  Map<String, dynamic> toJson() => {
        'hours': hours,
        'mdays': mdays,
        'minutes': minutes,
        'months': months,
        'wdays': wdays,
        'timezone': timezone,
      };
}

`

can you please have a look? if is it something I am doing wrong.

jabirmayar commented 1 year ago

Payload sending using dart is {"job":{"enabled":true,"schedule":{"timezone":"Asia/Karachi","hours":[3],"mdays":[-1],"minutes":[22],"months":[-1],"wdays":[-1]}}} and in python:

{"job": {"enabled": true, "schedule": {"timezone": "UTC", "hours": [-1], "mdays": [-1], "minutes": [-1], "months": [-1], "wdays": [-1]}}}

jabirmayar commented 1 year ago

closing this issue. as the issue seems to be with the dart official HTTP package. I tried with Dio and it worked with it.