Skip to content

06-content-negotiation, "content-type" required when encoding is "gzip" #20

Description

@AkatQuas

When accept-encoding is gzip, we need to set the content-type to text/plain.

this.response.set('Content-Encoding', 'gzip');
this.response.body = yield gzip('hello world');

The above code would fail the test because, when content-enconding is gzip, the default content-type is application/octet-stream.

Adding the content-type assignment would solve the problem.

this.response.set('Content-Encoding', 'gzip');
this.response.set('content-type', 'text/plain');
this.response.body = yield gzip('hello world');
It takes me a lot time to understand what this chapter is focusing on.

Here is one solution in case anyone need.

app.use(function* () {
  const ae = this.request.acceptsEncodings('gzip', 'identity');

  switch (ae) {
    case 'gzip':
      this.response.set('Content-Encoding', 'gzip');
      this.response.set('content-type', 'text/plain');
      this.response.body = yield gzip('hello world');
      break;
    case 'identity':
      this.response.set('Content-Encoding', 'identity');
      this.response.body = 'hello world';
      break;
    default:
      break;
  }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions