Skip to content

Latest commit

 

History

History
101 lines (68 loc) · 2.41 KB

requests.md

File metadata and controls

101 lines (68 loc) · 2.41 KB

Requests

Path parameters

Use {}to specify the name and the position of your parameter directly in the url

@Get(path: '/{id}')

Then bind it to your method using the Path annotation.

Future<Response> getById(@Path() String id);

or

Future<Response> getById(@Path('id') int ref);

Chopper will use the toString method to concat the url with the parameter.

Query parameters

Use the Query annotation to add query parameters to the url

Future<Response> search({
    @Query() String name,
    @Query('int') int number,
    @Query('default_value') int def = 42,
});

If you prefer to pass to pass a full Map you can use the QueryMap annotation

Future<Response> search(@QueryMap() Map<String, dynamic> query);

Request body

Use Body annotation to specify data to send.

@Post(path: "post")
Future<Response> postData(@Body() String data);

{% hint style="warning" %} Chopper does not automatically convert Object to Mapthen JSON

A Converter is necessary to do that, see built_value_converter for more infos. {% endhint %}

Headers

Request headers can be set using Interceptor or Converter, but also on the Method definition.

@Get(path: '/', headers: {'foo': 'bar'})
Future<Response> fetch();

/// dynamic
@Get(path: '/')
Future<Response> fetch(@Header('foo') String bar);

Send application/x-www-form-urlencoded

If no converter specified and if you are just using Map<String, String> as body. This is the default behavior of the http package.

You can also use FormUrlEncodedConverter that will add the correct content-type and convert simple Map into Map<String, String> to all request.

final chopper = ChopperClient(
      converter: FormUrlEncodedConverter(),
);

On single method

If you wish to do a form urlencoded request on a single request, you can use a factory converter.

@Post(path: 'form', headers: {contentTypeKey: formEncodedHeaders})
Future<Response> postForm(@Body() Map<String, String> fields);

Use Field annotation

To specify each fields manually, use the Field annotation.

  @FactoryConverter(request: FormUrlEncodedConverter.requestFactory)
  @Post(path: 'form')
  Future<Response> post(@Field() String foo, @Field('b') int bar);