Make HTTP Requests to the Web API from .NET Applications
从服务器向webAPI发送请求
The examples below send requests to the Web API service available at the following address: https://localhost:44319/
.
#Get a JWT Authentication Token/拿到token
To obtain the JWT Authentication token for further data requests, send a request to the following endpoint: api/Authentication/Authenticate
. The following example uses “Sam” as a user name and an empty password:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class Program {
static async Task Main(string[] args) {
HttpClient httpClient = new HttpClient();
// Obtain a JWT token.
StringContent httpContent = new StringContent(@"{ ""userName"": ""Sam"", ""password"": """" }", Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://localhost:44319/api/Authentication/Authenticate", httpContent);
// Save the token for further requests.
var token = await response.Content.ReadAsStringAsync();
// Set the authentication header.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
}
}
#Operate with Business Objects
#Get Business Objects/获取业务代码 get
The following code retrieves the LastName and Email fields of the Employee business object where FirstName equals “Mary”:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class Program {
static async Task Main(string[] args) {
HttpClient httpClient = new HttpClient();
// Obtain a JWT token. This example uses "Sam" as a user name and an empty password.
StringContent httpContent = new StringContent(@"{ ""userName"": ""Sam"", ""password"": """" }", Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://localhost:44319/api/Authentication/Authenticate", httpContent);
// Save the token for further requests.
var token = await response.Content.ReadAsStringAsync();
// Set the authentication header.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
// Send a request to fetch data.
string requestAddress = "https://localhost:44319/api/odata/Employee";
var employees = await httpClient.GetStringAsync($"{requestAddress}?$filter=FirstName eq 'Mary'&$select=LastName,Email");
Console.WriteLine(employees);
}
}
}
Result
{"@odata.context":"https://localhost:44319/api/odata/$metadata#Employee(LastName,Email)",
"value":[{"LastName":"Tellitson",
"Email":"Mary_Tellitson@example.com"}]}
#Create a Business Object/创建业务对象
The code below adds a new Employee instance with the FirstName field set to “Mary” and the LastName field set to “Gordon”:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class Program {
static async Task Main(string[] args) {
HttpClient httpClient = new HttpClient();
// Obtain a JWT token. This example uses "Sam" as a user name and an empty password.
StringContent httpContent = new StringContent(@"{ ""userName"": ""Sam"", ""password"": """" }", Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://localhost:44319/api/Authentication/Authenticate", httpContent);
// Save the token for further requests.
var token = await response.Content.ReadAsStringAsync();
// Set the authentication header.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
// Pass data to the Web API service.
StringContent dataHttpContent = new StringContent(@"{ ""FirstName"": ""Mary"", ""LastName"":""Gordon"" }", Encoding.UTF8, "application/json");
var dataResponse = await httpClient.PostAsync($"{requestAddress}", dataHttpContent);
Console.WriteLine(dataResponse.StatusCode);
}
}
}
Result (the dataResponse.StatusCode value): 201 Created
#Get a Reference Object/获取参考对象
有以下2个办法
#Technique 1 (Expand Query Parameter)
The example below uses $expand to get an Employee object with its related Department object:
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class Program {
static async Task Main(string[] args) {
HttpClient httpClient = new HttpClient();
// Obtain a JWT token. This example uses "Sam" as a user name and an empty password.
StringContent httpContent = new StringContent(@"{ ""userName"": ""Sam"", ""password"": """" }", Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://localhost:44319/api/Authentication/Authenticate", httpContent);
// Save the token for further requests.
var token = await response.Content.ReadAsStringAsync();
// Set the authentication header.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
// Send a request to fetch data.
string requestAddress = "https://localhost:44319/api/odata/Employee";
var employees = await httpClient.GetStringAsync($"{requestAddress}?$filter=FirstName eq 'Mary'&$select=LastName,Email&$expand=Department");
Console.WriteLine(employees);
}
}
}
Result
{"@odata.context":"https://localhost:44319/api/odata/$metadata#Employee(LastName,Email,Department())",
"value":[{"LastName":"Tellitson",
"Email":"Mary_Tellitson@example.com",
"Department":{
"Oid":"6eff292f-f871-4237-a22c-8a50aa747ea3",
"Title":"Development Department",
"Description":"The Information Technology Department manages the company's information infrastructure and online assets.",
"Location":"Building 2",
"Office":"205"}
}]
}
The $expand parameter can be applied to more than one level of related business objects. The following example retrieves a data chain that consists of two related business objects:
// ...
string requestAddress = "https://localhost:44319/api/odata/Department";
var departments = await httpClient.GetStringAsync($"{requestAddress}?$select=Title&$expand=Employees($select=FirstName,LastName;$expand=Tasks($select=Subject))");
// ...
Result
{"@odata.context": "https://localhost:44319/api/odata/$metadata#Department(Title,Employees(FirstName,LastName,Tasks(Subject)))",
"value": [{ "Title": "Human Resources",
"Employees": [{ "FirstName": "Angela",
"LastName": "Gross",
"Tasks": [{ "Subject": "Create 2022 R&D Plans"},
{ "Subject": "Submit D&B Number to ISP for Credit Approval"},
{ "Subject": "Deliver R&D Plans for 2022"}]
},
{ "FirstName": "Barbara",
"LastName": "Faircloth",
"Tasks": [{ "Subject": "Subject": "Deliver R&D Plans for 2022"},
{ "Subject": "Submit D&B Number to ISP for Credit Approval"},
{ "Subject": "Create 2022 R&D Plans"}]
}]
},
{ "Title": "Purchasing",
"Employees": [{ "FirstName": "Ernest",
"LastName": "Webb",
"Tasks": [{ "Subject": "Submit D&B Number to ISP for Credit Approval"},
{ "Subject": "Deliver R&D Plans for 2022"}]
},
...
]
}]
}
The default max expansion depth equals two. You can change this parameter as described in the following topic: Change the Expansion Depth for Related Business Objects.
#Technique 2 (Ref Endpoint)
The example below gets the Employee’s Department reference object:
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class Program {
static async Task Main(string[] args) {
HttpClient httpClient = new HttpClient();
// Obtain a JWT token. This example uses "Sam" as a user name and an empty password.
StringContent httpContent = new StringContent(@"{ ""userName"": ""Sam"", ""password"": """" }", Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://localhost:44319/api/Authentication/Authenticate", httpContent);
// Save the token for further requests.
var token = await response.Content.ReadAsStringAsync();
// Set the authentication header.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
// Send a request to fetch data.
string requestAddress = "https://localhost:44319/api/odata/Employee/1/Department/$ref";
// or
// string requestAddress = "https://localhost:44319/api/odata/Employee(1)/Department/$ref";
var department = await httpClient.GetStringAsync(requestAddress);
Console.WriteLine(department);
}
}
}
Result
{"@odata.context":"https://localhost:44319/api/odata/$metadata#Department/$entity",
"ID":1,
"Title":"Development Department",
"Office":"205","Location":"Building 2",
"Description":"The Information Technology Department manages the company's information infrastructure and online assets."}
#Get an Associated Collection
有以下2个办法
#Technique 1 (Expand Query Parameter)
The following example gets LastName, Email, and the related Tasks collection of the Employee business object where FirstName equals “Mary”:
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class Program {
static async Task Main(string[] args) {
HttpClient httpClient = new HttpClient();
// Obtain a JWT token. This example uses "Sam" as a user name and an empty password.
StringContent httpContent = new StringContent(@"{ ""userName"": ""Sam"", ""password"": """" }", Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://localhost:44319/api/Authentication/Authenticate", httpContent);
// Save the token for further requests.
var token = await response.Content.ReadAsStringAsync();
// Set the authentication header.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
// Send a request to fetch data.
string requestAddress = "https://localhost:44319/api/odata/Employee";
var employees = await httpClient.GetStringAsync($"{requestAddress}?$filter=FirstName eq 'Mary'&$select=LastName,Email&$expand=Tasks");
Console.WriteLine(employees);
}
}
}
Result
{"@odata.context":"https://localhost:44319/api/odata/$metadata#Employee(LastName,Email,Tasks())",
"value":[{"LastName":"Tellitson",
"Email":"Mary_Tellitson@example.com",
"Tasks":[{"Oid":"b958f20a-118d-4af0-b249-94445608549d",
"Subject":"2022 Brochure Designs",
"DueDate":"2022-01-15T00:00:00+04:00",
"StartDate":"0001-01-01T00:00:00Z",
"Status":"Deferred",
"PercentCompleted":0,
"Priority":"Normal"},
{"Oid":"7de87fc8-4dc0-4b76-82b3-18dffdc61ba4",
"Subject":"Review Benefits",
"DueDate":"2021-10-02T00:00:00+04:00",
"StartDate":"2021-09-12T00:00:00+04:00",
"Status":"Completed",
"PercentCompleted":100,
"Priority":"Normal"},
{"Oid":"67d36cda-a261-489f-afa9-c8ac43e1c2ea",
"Subject":"Lunch Potluck",
"DueDate":"2021-10-03T00:00:00+04:00",
"StartDate":"0001-01-01T00:00:00Z",
"Status":"Deferred",
"PercentCompleted":0,
"Priority":"Low"}
]
}]
}
#Technique 2 (Ref Endpoint)
The example below gets the Employee’s Tasks collection:
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class Program {
static async Task Main(string[] args) {
HttpClient httpClient = new HttpClient();
// Obtain a JWT token. This example uses "Sam" as a user name and an empty password.
StringContent httpContent = new StringContent(@"{ ""userName"": ""Sam"", ""password"": """" }", Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://localhost:44319/api/Authentication/Authenticate", httpContent);
// Save the token for further requests.
var token = await response.Content.ReadAsStringAsync();
// Set the authentication header.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
// Send a request to fetch data.
string requestAddress = "https://localhost:44319/api/odata/Employee(1)/Tasks/$ref";
// or
// string requestAddress = "https://localhost:44319/api/odata/Employee/1/Tasks/$ref";
var tasks = await httpClient.GetStringAsync(requestAddress);
Console.WriteLine(tasks);
}
}
}
Result
{"@odata.context":"https://localhost:44319/api/odata/$metadata#DemoTask",
"value":[
{
"ID":8,
"Subject":"Approve Overtime Pay",
"Description":"Brett, the overtime I submitted was not paid and I'm being told it was not approved. I thought you approved this. What is the problem?\r\nBrett Wade: I did approve it. It was error in payroll. Trying to figure it out.",
"DueDate":"2022-04-22T00:00:00+04:00",
"StartDate":"2022-03-28T00:00:00+04:00",
"PercentCompleted":0,
"Status":"Completed",
"Priority":"Normal",
"ActualWorkHours":15,
"EstimatedWorkHours":19
},
{
"ID":9,
"Subject":"Move Inventory to New Warehouse",
"Description":"Robin, you are point person to get all inventory moved to the new warehouse location. You can hire temp workers if needed.",
"DueDate":"2022-04-24T00:00:00+04:00",
"StartDate":null,
"PercentCompleted":0,
"Status":"NotStarted",
"Priority":"Low",
"ActualWorkHours":0,
"EstimatedWorkHours":10
},
{
"ID":10,
"Subject":"Shipping Label Artwork",
"Description":"Kevin wants new shipping labels and I cannot print them without the artwork from your team. Can you please hurry and send it to me.\r\nMorgan Kennedy: Send me the specs and I will work on it when I can.",
"DueDate":"2022-04-24T00:00:00+04:00",
"StartDate":"2022-04-19T00:00:00+04:00",
"PercentCompleted":0,
"Status":"InProgress",
"Priority":"High",
"ActualWorkHours":19,
"EstimatedWorkHours":12
}
]}
#Assign an Object to a Reference Property
The example below sets an Employee’s Department reference property to a Department object:
#Technique 1 (In Body) - XPO Only
string requestAddress = "https://localhost:44318/api/odata/Employee/1";
string jsonBody = @"{ ""Department"": ""1""}";
// or
// string jsonBody = @"{ ""Department"": { ""Oid"": ""1"" }}";
StringContent content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var response = await httpClient.PatchAsync(requestAddress, content);
Console.WriteLine(response);
Result (the dataResponse.StatusCode value): 204 No Content
To clear a Reference property, send a null value. The example below assigns null to an Employee’s Department property:
string requestAddress = "https://localhost:44318/api/odata/Employee/1";
string jsonBody = @"{ ""Department"": null }";
StringContent content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var response = await httpClient.PatchAsync(requestAddress, content);
Console.WriteLine(response);
Result (the dataResponse.StatusCode value): 204 No Content
#Technique 2 (Ref Endpoint)
string requestAddress = "https://localhost:44319/api/odata/Employee/1/Department/$ref";
string jsonBody = "{\"@odata.id\":\"https://localhost:44319/api/odata/Department/1\"}";
StringContent content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var response = await httpClient.PutAsync(requestAddress, content);
Console.WriteLine(response);
Result (the dataResponse.StatusCode value): 204 No Content
#Add an Object to a Collection
#Technique 1 (In Body) - XPO Only
The example below adds a DemoTask object to an Employee’s Tasks collection:
string requestAddress = "https://localhost:44319/api/odata/Employee/1";
string jsonBody = @"{ ""Tasks"": [ { ""Oid"": 1 } ] }";
var response = await httpClient.PatchAsync(requestAddress, content);
Console.WriteLine(response);
Result (the dataResponse.StatusCode value): 204 No Content
#Technique 2 (Ref Endpoint)
The example below adds a DemoTask object to an Employee’s Tasks collection:
string requestAddress = "https://localhost:44319/api/odata/Employee/1/Tasks/$ref";
string jsonBody = "{\"@odata.id\":\"https://localhost:44319/api/odata/DemoTask(1)\"}";
StringContent content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(requestAddress, content);
Console.WriteLine(response);
Result (the dataResponse.StatusCode value): 204 No Content
#Unlink an Object from a Reference Property
The example below removes the Employee’s Department reference property value:
string requestAddress = @"https://localhost:44319/api/odata/Employee/1/Department/$ref?
$id=https://localhost:44319/api/odata/Department/1";
// or
// string requestAddress = @"https://localhost:44319/api/odata/Employee(1)/Department/$ref?
// $id=https://localhost:44319/api/odata/Department(1)";
var response = await httpClient.DeleteAsync(requestAddress);
Console.WriteLine(response)
Result (the dataResponse.StatusCode value): 204 No Content
#Remove an Object from a Collection
The example below removes the DemoTask object from the Employee’s Tasks collection:
string requestAddress = @"https://localhost:44319/api/odata/Employee/1/Tasks/$ref?
$id=https://localhost:44319/api/odata/DemoTask/1";
// or
// string requestAddress = @"https://localhost:44319/api/odata/Employee(1)/Tasks/$ref?
// $id=https://localhost:44319/api/odata/DemoTask(1)";
var response = await httpClient.DeleteAsync(requestAddress);
Console.WriteLine(response)
Result (the dataResponse.StatusCode value): 204 No Content
#Modify an Object Assigned to a Reference Property (XPO Only)
The example below modifies the Department object assigned to the Employee’s Department reference property:
string requestAddress = "https://localhost:44319/api/odata/Employee/1";
string jsonBody = @"{ ""Department"": { ""Office"":""504""} }";
StringContent content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var response = await httpClient.PatchAsync(requestAddress, content);
Console.WriteLine(response);
Result (the dataResponse.StatusCode value): 204 No Content
#Modify Objects Added to an Associated Collection (XPO Only)
The example below modifies the DemoTask object with Oid
=1 from the Employee’s Tasks collection. If the collection does not contain the DemoTask object with this Oid
, this object will be added:
string requestAddress = "https://localhost:44319/api/odata/Employee/1";
string jsonBody = @"{ ""Tasks"": [{ ""Oid"": ""1"", ""Subject"":""New subject""} ]}";
StringContent content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var response = await httpClient.PatchAsync(requestAddress, content);
Console.WriteLine(response);
Result (the dataResponse.StatusCode value): 204 No Content
文章来源: