How to use the types function from superagent

Find comprehensive JavaScript superagent.types code examples handpicked from public code repositorys.

superagent.types is a map of file extension to MIME type used for setting the Content-Type header in SuperAgent requests.

4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
/**
 * Set Content-Type to `type`, mapping values from `request.types`.
 *
 * Examples:
 *
 *      superagent.types.xml = 'application/xml';
 *
 *      request.post('/')
 *        .type('xml')
 *        .send(xmlstring)
fork icon11
star icon13
watch icon0

+ 5 other calls in file

How does superagent.types work?

superagent.types is a property in the SuperAgent library that provides a way to associate a file type with a MIME type, allowing automatic conversion of response bodies to the specified file type.

Ai Example

1
2
3
4
5
6
7
8
9
10
const request = require("superagent");
const { types } = request;

request
  .post("/api/data")
  .send({ name: "John", age: 25 })
  .type(types.json)
  .then((res) => {
    console.log("Response:", res.body);
  });

In this example, we use the type method to set the request content type to JSON, which is defined in the types object of the superagent module.