import esriRequest from "@arcgis/core/request.js";
const esriRequest = await $arcgis.import("@arcgis/core/request.js");
@arcgis/core/request
Retrieves data from a remote server or uploads a file.
// request GeoJson data from USGS remote server
let url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson";
esriRequest(url, {
responseType: "json"
}).then((response) => {
// The requested data
let geoJson = response.data;
});
Method Overview
| Name | Return Type | Summary | Function |
|---|---|---|---|
Promise<RequestResponse> | Retrieves data from a remote server or uploads a file from a user's computer. | request |
Method Details
-
request
MethodesriRequest.request(url, options){Promise<RequestResponse>} -
Retrieves data from a remote server or uploads a file from a user's computer. If the request returns an Error, the error object will include the details specified in EsriErrorDetails.
ParametersThe request URL.
options RequestOptionsoptionalThe options specified by the user in the data request. See RequestOptions for available properties.
ReturnsType Description Promise<RequestResponse> Returns a promise that resolves to an object with the RequestResponse specification. If the request returns an Error, the error object will include the details specified in EsriErrorDetails.
Type Definitions
-
The specification of the details object returned in an Error object.
- Properties
-
getHeader getHeader
A function to retrieve headers sent from the server.
getAllHeaders getAllHeadersA function for retrieving all headers sent from the server.
httpStatus NumberThe status code of the http response.
messageCode StringThe error message code.
Additional error message(s).
The raw error object if the server returned a JSON error, or the response text otherwise.
requestOptions RequestOptionsThe options used in the http request.
ssl BooleanIndicates if the request required https.
subCode NumberThe error message subcode.
url StringThe URL of the request that returned an error message.
-
An object with the following properties that describe the request.
- Properties
-
optionalauthMode StringDefault Value:auto
Indicates if and how requests to ArcGIS Services are authenticated. Only applicable when
esriConfig.request.useIdentity = true.Known Value Description auto The user will be signed in when a secure resource is requested. anonymous An error will be returned when a secure resource is requested. immediate The user will be signed in before the resource is requested. no-prompt Checks for whether the user is already signed in. If so, no additional prompts display for sign-in. Note: This is not supported when used in a custom worker that is not using the workers framework.
Possible Values:"auto"|"anonymous"|"immediate"|"no-prompt"
optional If uploading a file, specify the form data or element used to submit the file here. If specified, the parameters of the
querywill be added to the URL.optionalcacheBust BooleanDefault Value:falseIf
true, the browser will send a request to the server instead of using the browser's local cache. Iffalse, the browser's default cache handling will be used.optionalheaders ObjectHeaders to use for the request. This is an object whose property names are header names.
optionalkeepAlive BooleanDefault Value:falseIndicates whether the browser will keep the associated request alive if the page that initiated it is unloaded before the request is complete, such as during page navigation or closing. See also
keepalivefor more details.optionalmethod StringDefault Value:autoIndicates if the request should be made using the HTTP DELETE, HEAD, POST, or PUT method. By default, HTTP POST will be used for
autoif the request size is longer than themaxUrlLengthproperty set in config.request.Possible Values:"auto"|"delete"|"head"|"post"|"put"
optionalquery Object|URLSearchParamsDefault Value:nullQuery parameters for the request. The query parameters will be added to the URL if a GET request is used, or if the
bodyproperty is set. If thebodyproperty is not set, the query parameters will be added to the request body when a DELETE, POST, or PUT request is used.optionalresponseType StringDefault Value:jsonResponse format. For details on the specific types, see: json, text, array-buffer, blob, image, native, native-request-init, document, and xml
Possible Values:"json"|"text"|"array-buffer"|"blob"|"image"|"native"|"native-request-init"|"document"|"xml"
optionalsignal AbortSignal|null|undefinedAbortSignal allows for cancelable requests. If canceled, the promise will be rejected with an error named
AbortError. See also AbortController.Example:
const controller = new AbortController(); const signal = controller.signal; esriRequest(url, { signal }) .then((response) => { // The request went OK }) .catch((err) => { if (err.name === 'AbortError') { console.log('Request aborted'); } else { console.error('Error encountered', err); } }); // Abort requests that are aware of the controller's signal controller.abort();optionaltimeout NumberDefault Value:62000Indicates the amount of time in milliseconds to wait for a response from the server. Set to
0to wait for the response indefinitely.optionaluseProxy BooleanDefault Value:falseIndicates the request should use the proxy. By default, this is determined automatically based on the domain of the request URL.
optionalwithCredentials BooleanDefault Value:falseIndicates if cross-site
Access-Controlrequests should use credentials. It is also possible to push the domain to the configtrustedServersif an application requires credentials. For additional information onwithCredentials, please refer to this documentation.
-
Returns a promise that resolves to an object with the following specification. If the request returns an Error, the error object will include the details specified in EsriErrorDetails.
- Properties
-
data *
The requested data. This value should match the RequestOptions
responseTypewith the data return type. Possible types are: json, text, array-buffer, blob, image, native, native-request-init, document, and xml.optionalgetHeader getHeaderMethod for getting a header sent from the server.
optionalgetAllHeaders getAllHeadersMethod for getting all headers sent from the server.
optionalhttpStatus NumberSince 4.26 The status code of the http response.
optionalrequestOptions RequestOptionsThe options specified by the user in the data request. See RequestOptions for available properties.
optionalssl BooleanIndicates if the request required https.
optionalurl StringThe URL used to request the data.
Example// request GeoJson data from USGS remote server let url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson"; esriRequest(url, { responseType: "json" }).then((response) => { // The requested data let geoJson = response.data; });
-
A function to retrieve all headers sent from the server. CORS only allows a few response headers to be read by default, see: Access-Control-Expose-Headers.
ReturnsExampleesriRequest(url, options) .then((response) => { console.log("All request headers: ", response.getAllHeaders()); });
-
A function to retrieve headers sent from the server. CORS only allows a few response headers to be read by default, see: Access-Control-Expose-Headers.
ParameterheaderName StringThe name of the header.
ReturnsExampleesriRequest(url, options) .then((response) => { // prints the content type of the request: 'application/json' console.log("header: ", response.getHeader('Content-Type')); });