|
| 1 | +--- |
| 2 | +--- |
| 3 | + |
| 4 | +## Request |
| 5 | + |
| 6 | +Make REST api calls to APIs that are not explicitly supported by JSForce. |
| 7 | + |
| 8 | +### Setting the URL |
| 9 | + |
| 10 | +The Endpoint URL can be in one of the following formats: |
| 11 | + |
| 12 | +- Absolute URL: `https://na1.salesforce.com/services/data/v32.0/sobjects/Account/describe`. |
| 13 | +- Relative path from root: `/services/data/v32.0/sobjects/Account/describe`. |
| 14 | +- Relative path from version root: `/sobjects/Account/describe`. |
| 15 | + - This is only supported if you have explicitly set a default version. |
| 16 | + |
| 17 | +### Making Requests |
| 18 | + |
| 19 | +You can use `Connection.request()` to make api requests. |
| 20 | + |
| 21 | +For GET requests, you can pass in a string URL. |
| 22 | + |
| 23 | +```javascript |
| 24 | +/* @interactive */ |
| 25 | +const res = await conn.request('/services/data/v47.0/ui-api/object-info'); |
| 26 | +console.log(res) |
| 27 | +``` |
| 28 | + |
| 29 | +If you prefer to use callbacks instead of promises, pass a callback as the second parameter. |
| 30 | + |
| 31 | +```javascript |
| 32 | +const res = await conn.request('/services/data/v47.0/ui-api/object-info'); |
| 33 | +console.log(res) |
| 34 | +``` |
| 35 | + |
| 36 | +For other HTTP methods, you can pass an object to the request method. Ensure that you serialize the body of the request. |
| 37 | + |
| 38 | +```javascript |
| 39 | +/* @interactive */ |
| 40 | +// Bulk API 2.0 - Query |
| 41 | +const requestBody = { |
| 42 | + operation: 'query', |
| 43 | + query: 'SELECT Id, Name FROM Account LIMIT 1000', |
| 44 | +}; |
| 45 | + |
| 46 | +const res = await conn |
| 47 | + .request({ |
| 48 | + method: 'POST', |
| 49 | + url: '/services/data/v47.0/jobs/query', |
| 50 | + body: JSON.stringify(requestBody), |
| 51 | + headers: { |
| 52 | + 'content-type': 'application/json', |
| 53 | + }, |
| 54 | + }); |
| 55 | +console.log(res) |
| 56 | +``` |
| 57 | + |
| 58 | +#### Request Helper Methods |
| 59 | + |
| 60 | +In addition to `Connection.request()`, JSForce provides the following helper methods that can also be used: |
| 61 | + |
| 62 | +- `Connection.requestGet()` |
| 63 | +- `Connection.requestPatch()` |
| 64 | +- `Connection.requestPost()` |
| 65 | +- `Connection.requestPut()` |
| 66 | +- `Connection.requestDelete()` |
| 67 | + |
| 68 | +For `requestPatch`, `requestPost` and `requestPut`, these will be serialized automatically and the `content-type` will be set to `application/json`. |
| 69 | + |
| 70 | +```javascript |
| 71 | +/* @interactive */ |
| 72 | +const requestBody = { |
| 73 | + operation: 'query', |
| 74 | + query: 'SELECT Id, Name FROM Account LIMIT 1000', |
| 75 | +}; |
| 76 | + |
| 77 | +const res = await conn.requestPost('/services/data/v47.0/jobs/query', requestBody); |
| 78 | +console.log(res); |
| 79 | +``` |
| 80 | + |
| 81 | +#### Request HTTP Options |
| 82 | + |
| 83 | +All request methods allow setting HTTP options to be passed to the HTTP request. |
| 84 | + |
| 85 | +| Name | Type | Description | |
| 86 | +| ----------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 87 | +| responseType | string | overrides the content-type from the response to change how the response is parsed. Valid values are `text/xml`, `application/xml`, `application/json`, `text/csv`. If you do not want JSForce to auto-parse the response, set this to any other value, e.x. `text`. | |
| 88 | +| noContentResponse | any | Alternative response when no content returned in response (= HTTP 204) | |
| 89 | +| transport | Transport | Transport for http api - you should not need to set this option. | |
| 90 | + |
| 91 | +If you would like to opt-out of parsing, you can set the `responseType` to text. This is useful if you want the raw response from Salesforce instead of having the results automatically parsed. |
| 92 | + |
| 93 | +```javascript |
| 94 | +// Get raw CSV data instead of allowing JSForce to parse the CSV to JSON |
| 95 | +const res = await conn.requestGet('/services/data/v47.0/jobs/query/7502J00000LYZC4QAP/results', { responseType: 'text' }); |
| 96 | +console.log(res); |
| 97 | +``` |
0 commit comments