| Argument | Description | Types / Required |
| fetchId | The resource ID returned by Network.fetch. | number
recommended |
| bodyFormat | The format in which the body is returned.
The default value is "dataUrl". | string
not required |
| cancel | A flag indicating that the body can be discarded. | boolean
not required |
The
Network.fetchBody action processes the response of a HTTP request initiated by
Network.fetch. The pending response is identified using
fetchId, a
resource ID returned by
Network.fetch. While optional, this should generally be specified explicitly. (If there is only one pending response, the resource ID can be calculated implicitly.) Once the response is processed, the resource ID is no longer valid.
The argument
cancel, if set to
true, results in the body of the response being discarded. If you want to conditionally process a response, for example based on the value of a header, this is more efficient than retrieving the entire response using an action such as
Network.fetchJson.
If retrieving the response, the argument bodyFormat determines how the body is returned. Options include:
- "json": The response body is parsed as JSON, returning a string, number, boolean, array, or object. This is similar to Network.fetchJson.
- "text": The response is returned as text. This is similar to Network.fetchText.
- If bodyFormat is specified and not one of the above options, it is used to specify the format of the returned binary data. If requesting a large amount of data, consider using the "cache" format to avoid representing a huge number of bytes as a string. This is similar to Network.fetchBytes.
- If no bodyFormat is specified, but the MIME type of the response is JSON or text-based, then it will be returned as JSON or text.
- If none of the above rules apply, the response defaults to a data URL.
The action returns the response body in the format specified. When discarding the body using cancel, the action returns undefined.
The action throws an exception if any of its arguments are not specified in a valid form, or if:
- The fetch ID is not valid.
- The connection times out.
- The response is not valid for its type (e.g., invalid JSON).
// query information about the latest CSPro commit;
// the GitHub API requires the specification of a User-Agent header
const response = CS.Network.fetch({
url: "https://api.github.com/repos/csprousers/cspro/commits?per_page=1",
headers: [
"User-Agent: CSPro Help Documentation Example"
]
});
if (response.ok) {
// with a successful response, retrieve the body as JSON using the
// resource ID (fetchId) returned from the initial call to Network.fetch
const commits = CS.Network.fetchBody({
fetchId: response.fetchId,
bodyFormat: "json"
});
const latestCommit = commits[0].commit;
console.log(`Latest commit authored by "${latestCommit.author.name}", ` +
`committed on "${latestCommit.committer.date}", ` +
`with message: "${latestCommit.message"}"`);
}