尝试对连接到 JSONAPI 的 SDK 进行 POST 调用
Trying to do a POST call for an SDK connecting to JSONAPI
我正在尝试创建一个 SDK 来连接使用 jsonapi (https://jsonapi.org/) 完成的 API,但是我在尝试执行 POST 请求时遇到了问题。
我有一些 GET 调用,但我不知道如何实现 POST。有没有我能看到的例子? (我在文档中找不到)
我有 GET:
public async Task<Clinic[]> GetClinicsAsync(int page = 1)
{
var uri = $"api/api-clinics".AddQueryParams(new Dictionary<string, string?> {
["page"] = page.ToString(),
}
);
return await _http.SendAsyncInternal<Clinic[]>(HttpMethod.Get, uri, new Dictionary<string, string>()) ?? Array.Empty<Clinic>();
}
具有这样的界面:
/// 获取诊所列表。
Task<Clinic[]> GetClinicsAsync(int page);
但是形成了我的例子 post 我有这个:
https://journalapi.website.de/api/api-bookings?caregiver_id=43&patient_personal_id=435496
PAYLOAD {"data":{"attributes":{"booked_by_patient":true,"class":"PUBLIC","description":"description text","dtend":"2022-03-09T10:00:00+01:00","dtstamp":"2022-03-08T08:10:27+01:00","dtstart":"2022-03-09T09:30:00+01:00","location":"1231, 31311 3131","new_patient":true,"referral_source":"","status":"CONFIRMED","summary":"summary text","text":"","transp":"OPAQUE","uid":null,"first_name":"","last_name":"","e_mail_address":"","phone_number_cell":""},"relationships":{"clinic":{"data":{"id":"2312","type":"api-clinics"}},"organizer":{"data":{"id":"5553","type":"api-caregivers"}},"procedure":{"data":{"id":"1","type":"api-procedure"}}},"type":"api-bookings"},"include":"booking_attendees.patient"}
不知道这个payload应该怎么处理:
public async Task CreateBookingAsync(string clinicId, string caregiverId, string procedureId, string id,
DateTimeOffset start, DateTimeOffset end)
{
var uri = "api/api-bookings".AddQueryParams(new Dictionary<string, string?> {
["caregiver_id"] = caregiverId,
["patient_personal_id"] = id
}
);
var body = new DocumentRoot<BookingRequest>();
body.Included = new List<JObject> { new JObject("test") };
}
编辑:添加尝试:
public async Task<string> CreateBookingAsync(string clinicId, string procedureId, string swedishPersonalIdentityNumber,
DateTimeOffset start, DateTimeOffset end, Booking booking)
{
var uri = "api/api-bookings".AddQueryParams(new Dictionary<string, string?> {
["caregiver_id"] = clinicId,
["patient_personal_id"] = swedishPersonalIdentityNumber
}
);
var jsonData = JsonConvert.SerializeObject(booking);
var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
var response = _http.PostAsync(uri, content).Result;
var body = new DocumentRoot<BookingRequest>
{
Included = new List<JObject> { new JObject("test") }
};
return await response.Content.ReadAsStringAsync();
}
用最简单的术语来说,您可以通过首先序列化 Model
然后将其发送到您的端点来 POST
调用您的 API。
根据您的输入,您的 Model
看起来像:
public class PayloadData
{
public string caregiver_id {get;set;}
public string patient_personal_id {get;set;}
}
您的通话将如下所示:
public async Task CreateBookingAsync(string clinicId, string caregiverId, string procedureId, string id, DateTimeOffset start, DateTimeOffset end)
{
PayloadData data=new PayloadData();
data.caregiver_id=caregiverId
data.patient_personal_id=id
var uri = "api/api-bookings";
string jsonData = JsonConvert.SerializeObject(data);
StringContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = _http.PostAsync(uri, content).Result;
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
//Deserialize your result as required
//var myFinalData = JsonConvert.DeserializeObject<dynamic>(result);
}
//Your logic
}
我正在尝试创建一个 SDK 来连接使用 jsonapi (https://jsonapi.org/) 完成的 API,但是我在尝试执行 POST 请求时遇到了问题。 我有一些 GET 调用,但我不知道如何实现 POST。有没有我能看到的例子? (我在文档中找不到)
我有 GET:
public async Task<Clinic[]> GetClinicsAsync(int page = 1)
{
var uri = $"api/api-clinics".AddQueryParams(new Dictionary<string, string?> {
["page"] = page.ToString(),
}
);
return await _http.SendAsyncInternal<Clinic[]>(HttpMethod.Get, uri, new Dictionary<string, string>()) ?? Array.Empty<Clinic>();
}
具有这样的界面:
/// 获取诊所列表。
Task<Clinic[]> GetClinicsAsync(int page);
但是形成了我的例子 post 我有这个:
https://journalapi.website.de/api/api-bookings?caregiver_id=43&patient_personal_id=435496
PAYLOAD {"data":{"attributes":{"booked_by_patient":true,"class":"PUBLIC","description":"description text","dtend":"2022-03-09T10:00:00+01:00","dtstamp":"2022-03-08T08:10:27+01:00","dtstart":"2022-03-09T09:30:00+01:00","location":"1231, 31311 3131","new_patient":true,"referral_source":"","status":"CONFIRMED","summary":"summary text","text":"","transp":"OPAQUE","uid":null,"first_name":"","last_name":"","e_mail_address":"","phone_number_cell":""},"relationships":{"clinic":{"data":{"id":"2312","type":"api-clinics"}},"organizer":{"data":{"id":"5553","type":"api-caregivers"}},"procedure":{"data":{"id":"1","type":"api-procedure"}}},"type":"api-bookings"},"include":"booking_attendees.patient"}
不知道这个payload应该怎么处理:
public async Task CreateBookingAsync(string clinicId, string caregiverId, string procedureId, string id,
DateTimeOffset start, DateTimeOffset end)
{
var uri = "api/api-bookings".AddQueryParams(new Dictionary<string, string?> {
["caregiver_id"] = caregiverId,
["patient_personal_id"] = id
}
);
var body = new DocumentRoot<BookingRequest>();
body.Included = new List<JObject> { new JObject("test") };
}
编辑:添加尝试:
public async Task<string> CreateBookingAsync(string clinicId, string procedureId, string swedishPersonalIdentityNumber,
DateTimeOffset start, DateTimeOffset end, Booking booking)
{
var uri = "api/api-bookings".AddQueryParams(new Dictionary<string, string?> {
["caregiver_id"] = clinicId,
["patient_personal_id"] = swedishPersonalIdentityNumber
}
);
var jsonData = JsonConvert.SerializeObject(booking);
var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
var response = _http.PostAsync(uri, content).Result;
var body = new DocumentRoot<BookingRequest>
{
Included = new List<JObject> { new JObject("test") }
};
return await response.Content.ReadAsStringAsync();
}
用最简单的术语来说,您可以通过首先序列化 Model
然后将其发送到您的端点来 POST
调用您的 API。
根据您的输入,您的 Model
看起来像:
public class PayloadData
{
public string caregiver_id {get;set;}
public string patient_personal_id {get;set;}
}
您的通话将如下所示:
public async Task CreateBookingAsync(string clinicId, string caregiverId, string procedureId, string id, DateTimeOffset start, DateTimeOffset end)
{
PayloadData data=new PayloadData();
data.caregiver_id=caregiverId
data.patient_personal_id=id
var uri = "api/api-bookings";
string jsonData = JsonConvert.SerializeObject(data);
StringContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = _http.PostAsync(uri, content).Result;
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
//Deserialize your result as required
//var myFinalData = JsonConvert.DeserializeObject<dynamic>(result);
}
//Your logic
}