Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added url encoded format for sending the data in request #554

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/codegen/dart/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ class DartDioCodeGen {
// when add new type of [ContentType], need update [dataExp].
case ContentType.formdata:
dataExp = declareFinal('data').assign(refer('dio.FormData()'));
case ContentType.urlencoded:
// Convert form data to URL-encoded format
final urlEncodedData = formData.map((item) =>
'${Uri.encodeComponent(item['name'] ?? '')}'
'='
'${Uri.encodeComponent(item['value'] ?? '')}'
).join('&');
dataExp = declareFinal('data').assign(literalString(urlEncodedData));
}
}
final responseExp = declareFinal('response').assign(InvokeExpression.newOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:apidash/providers/providers.dart';
import 'package:apidash/widgets/widgets.dart';
import 'package:apidash/consts.dart';
import 'request_form_data.dart';
import 'package:apidash/widgets/form_data_toggle.dart';

class EditRequestBody extends ConsumerWidget {
const EditRequestBody({super.key});
Expand Down Expand Up @@ -52,6 +53,8 @@ class EditRequestBody extends ConsumerWidget {
),
)
: kSizedBoxEmpty,
const FormDataToggle(),
const SizedBox(height: 16),
switch (apiType) {
APIType.rest => Expanded(
child: switch (contentType) {
Expand Down
112 changes: 112 additions & 0 deletions lib/widgets/form_data_toggle.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash_core/apidash_core.dart';
import '../providers/providers.dart';

class FormDataToggle extends ConsumerWidget {
const FormDataToggle({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
final selectedId = ref.watch(selectedIdStateProvider);
final contentType = ref.watch(selectedRequestModelProvider
.select((value) => value?.httpRequestModel?.bodyContentType));
final formData = ref.watch(selectedRequestModelProvider
.select((value) => value?.httpRequestModel?.formData));

final hasFiles = formData?.any((item) => item.type == FormDataType.file) ?? false;

return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 2),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const SizedBox(width: 6),
const Text('Form Data Type:'),
SegmentedButton<ContentType>(
segments: const [
ButtonSegment<ContentType>(
value: ContentType.urlencoded,
label: Text('Text Only'),
icon: Icon(Icons.text_fields),
),
ButtonSegment<ContentType>(
value: ContentType.formdata,
label: Text('Include Files'),
icon: Icon(Icons.attach_file),
),
],
selected: {contentType == ContentType.formdata ? ContentType.formdata : ContentType.urlencoded},
onSelectionChanged: (Set<ContentType> newSelection) {
final newContentType = newSelection.first;
if (newContentType == ContentType.urlencoded && hasFiles) {
// Show warning dialog when switching to urlencoded with files
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Warning'),
content: const Text(
'Switching to "Text Only" mode will remove any file attachments. '
'Are you sure you want to continue?'
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
ref
.read(collectionStateNotifierProvider.notifier)
.update(id: selectedId!, bodyContentType: newContentType);
},
child: const Text('Continue'),
),
],
),
);
} else {
ref
.read(collectionStateNotifierProvider.notifier)
.update(id: selectedId!, bodyContentType: newContentType);
}
},
),
const SizedBox(width: 6),
],
),
if (hasFiles && contentType == ContentType.urlencoded)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.errorContainer,
borderRadius: BorderRadius.circular(4),
),
child: Row(
children: [
Icon(
Icons.warning,
color: Theme.of(context).colorScheme.error,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'Files detected. Switch to "Include Files" mode to support file uploads.',
style: TextStyle(
color: Theme.of(context).colorScheme.error,
),
),
),
],
),
),
),
],
);
}
}
10 changes: 9 additions & 1 deletion packages/apidash_core/lib/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,20 @@ const kSubTypeDefaultViewOptions = 'all';
enum ContentType {
json("$kTypeApplication/$kSubTypeJson"),
text("$kTypeText/$kSubTypePlain"),
formdata("$kTypeMultipart/$kSubTypeFormData");
formdata("$kTypeMultipart/$kSubTypeFormData"),
urlencoded("$kTypeApplication/$kSubTypeXWwwFormUrlencoded");

const ContentType(this.header);
final String header;
}

Map<ContentType, String> kContentTypeMap = {
ContentType.json: ContentType.json.header,
ContentType.text: ContentType.text.header,
ContentType.formdata: ContentType.formdata.header,
ContentType.urlencoded: ContentType.urlencoded.header,
};

const JsonEncoder kJsonEncoder = JsonEncoder.withIndent(' ');
const JsonDecoder kJsonDecoder = JsonDecoder();
const LineSplitter kSplitter = LineSplitter();
Expand Down