REST Resources and Requests
原文
REST API is based on the usage of resources—pieces of data in Salesforce, such as records, collections of records, query results, metadata, or API information. Each resource is exposed by a uniform resource identifier (URI) and is accessed by sending HTTP requests to the corresponding URI.
Depending on which resource you want to access and how you construct an HTTP request, you can perform several types of operations, including:
Determine available API versions
Access limits for your Salesforce org
Retrieve object metadata
Create, read, update, and delete records
Query and search for data
You can send HTTP requests using a variety of software tools, which means that the exact appearance of a request can look different from the cURL examples in this guide. However, no matter how you submit requests, the elements don’t change. A typical request consists of these elements.
URI
HTTP method
Headers
Request body (not required for GET requests)
REST资源和请求
RESTAPI基于Salesforce中数据的资源片段的使用,例如记录、记录集合、查询结果、元数据或API信息。每个资源都由统一的资源标识符(URI)公开,并通过向相应的URI发送HTTP请求来访问。
根据要访问的资源和构造HTTP请求的方式,可以执行多种类型的操作,包括:
确定可用的API版本
Salesforce组织的访问限制
检索对象元数据
创建、读取、更新和删除记录
查询和搜索数据
您可以使用各种软件工具发送HTTP请求,这意味着请求的确切外观可能与本指南中的cURL示例不同。然而,无论您如何提交请求,元素都不会更改。典型的请求由这些元素组成。
URI(URI)
HTTP方法
邮件头
请求正文(GET请求不需要)
URIs
The URI is the path to a resource in Salesforce. Although the URI changes from resource to resource, the basic structure remains the same.
https://MyDomainName.my.salesforce.com/services/data/vXX.X/resource/
Use https:// to securely access resources.
Replace MyDomainName with the subdomain of your Salesforce org. Salesforce runs on multiple server instances, so the examples in this guide use MyDomainName in place of a specific instance.
Replace XX.X with the version of the API that you want to use. You can find a list of available versions by accessing the Versions resource.
Replace resource with the rest of the path to the resource. Depending on the resource, the path can contain parameters, such as IDs to identify a specific record. You can find the URIs for different resources in the Reference section of this guide.
sObject resources access standard and custom objects in Salesforce. For information about objects, see Object Reference for the Salesforce Platform.
Note
Some parts of URIs are case-sensitive, such as IDs. Other parts of URIs aren’t case-sensitive, such as object and field names. If your requests aren’t successful, check that your URI has the right letter cases by comparing the URI to the examples in this guide.
URI
URI是Salesforce中资源的路径。虽然URI会随着资源的不同而变化,但基本结构保持不变。
https://MyDomainName.my.salesforce.com/services/data/vXX.X/resource/
使用https://安全访问资源。
将MyDomainName替换为Salesforce组织的子域。Salesforce在多个服务器实例上运行,因此本指南中的示例使用MyDomainName代替特定实例。
更换XX。X和您要使用的API版本。通过访问版本资源,可以找到可用版本的列表。
用资源路径的其余部分替换资源。根据资源的不同,路径可以包含参数,例如标识特定记录的ID。您可以在本指南的参考部分中找到不同资源的URI。
sObject资源访问Salesforce中的标准和自定义对象。有关对象的信息,请参阅Salesforce Platform的对象引用。
笔记
URI的某些部分区分大小写,例如ID。URI的其他部分不区分大小写,例如对象和字段名。如果您的请求不成功,请将URI与本指南中的示例进行比较,以检查您的URI是否具有正确的字母大小写。
HTTP Methods
REST API supports standard HTTP request methods (HEAD, GET, POST, PATCH, PUT, and DELETE), which follow the specifications at https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html.
The purpose of each method varies depending on the resource. For information on how and when to use each method, check the page for that resource in the Reference section of this guide.
Headers
Use headers to pass parameters and customize options for HTTP requests. REST API supports several standard HTTP headers, as well as custom headers that are specific to Salesforce.
Common headers used in the examples include:
HTTP Accept—Indicates the format that your client accepts for the response body. Possible values are application/json and application/xml. The default value is application/json.
HTTP Content-type—Indicates the format of the request body that you attach to the request. Possible values are application/json and application/xml.
HTTP Authorization—Provides the OAuth 2.0 access token to authorize your client. REST API supports the Bearer authentication type.
Compression header—Compresses the request or the response. For more information, see Compression Headers.
Conditional request header—Validates the records that you access against a precondition. For more information, see Conditional Request Headers.
HTTP方法
REST API支持标准HTTP请求方法(HEAD、GET、POST、PATCH、PUT和DELETE),这些方法遵循https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html.
每种方法的用途因资源而异。有关如何以及何时使用每种方法的信息,请查看本指南“参考”部分中该资源的页面。
邮件头
使用标头传递参数并自定义HTTP请求的选项。REST API支持几个标准HTTP头,以及特定于Salesforce的自定义头。
示例中使用的常见标题包括:
HTTP接受指示客户端接受的响应正文格式。可能的值为application/json和application.xml。默认值为application/json。
HTTP内容类型指示附加到请求的请求正文的格式。可能的值为application/json和application.xml。
HTTP授权提供OAuth 2.0访问令牌来授权您的客户端。REST API支持Bearer身份验证类型。
压缩头压缩请求或响应。有关详细信息,请参阅压缩头。
条件请求头验证您根据先决条件访问的记录。有关更多信息,请参阅条件请求标头。
Request Bodies
A request body is a rich input that can be included in the request to provide additional information, such as field values for creating or updating records. A request body can be in JSON or XML format.
Note
Resources accessed with the GET method don’t require you to attach a request body.
Use the HTTP Content-type header to indicate the file format of the request body. If you use cURL to send the request, attach the JSON or XML file to the request using the —data-binary or -d option.
See Also
Send REST Requests with cURL
Setting Up Your Java Developer Environment
请求正文
请求正文是一个丰富的输入,可以包含在请求中以提供其他信息,例如用于创建或更新记录的字段值。请求正文可以是JSON或XML格式。
笔记
使用GET方法访问的资源不需要附加请求正文。
使用HTTP Content-type头来指示请求正文的文件格式。如果使用cURL发送请求,请使用-data binary或-d选项将JSON或XML文件附加到请求。
另请参见
使用cURL发送REST请求
设置Java开发人员环境