post static method

Future<PodResponse> post(
  1. String containerUrl,
  2. String name,
  3. String content, {
  4. PodContentType contentType = PodContentType.json,
  5. bool isDirectory = false,
})

Perform a POST request to create a resource.

Implementation

static Future<PodResponse> post(
  String containerUrl,
  String name,
  String content, {
  PodContentType contentType = PodContentType.json,
  bool isDirectory = false,
}) async {
  try {
    final tokens = await PodAuth.getTokens(containerUrl, 'POST');

    // Link header for resource type.

    const fileTypeLink = '<http://www.w3.org/ns/ldp#Resource>; rel="type"';
    const dirTypeLink =
        '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"';

    final response = await http.post(
      Uri.parse(containerUrl),
      headers: {
        'Accept': '*/*',
        'Authorization': 'DPoP ${tokens.accessToken}',
        'Connection': 'keep-alive',
        'Content-Type': isDirectory ? 'text/turtle' : contentType.value,
        'Link': isDirectory ? dirTypeLink : fileTypeLink,
        'Slug': name,
        'DPoP': tokens.dPopToken,
      },
      body: isDirectory ? '' : utf8.encode(content),
    );

    return PodResponse(
      statusCode: response.statusCode,
      body: response.body,
      headers: response.headers,
    );
  } catch (e) {
    debugPrint('PodHttp.post() error: $e');
    rethrow;
  }
}