Search code examples
c#apisdkjson-api

Trying to do a POST call for an SDK connecting to JSONAPI


I'm trying to create an SDK to connect for an API done with jsonapi (https://jsonapi.org/) , however I'm having problems trying to do POST request. I have some GET calls, but I don't know howe to implement the POST. is there any examples I could see? (I couldn't find in the documentation)

I have for 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>();
        }

with an interface like this:

/// Get the list of clinics.

Task<Clinic[]> GetClinicsAsync(int page);

but form the example I have for a post I have this:

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"}

I don't know how this payload should be dealt with:

     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") };
}

Edit: adding attempt:

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();
        }

Solution

  • In its simplest terms, you can make POST call to your API by first serializing your Model and then sending it to your endpoint.

    Your Model will look like based on your input:

    public class PayloadData
    {  
        public string caregiver_id {get;set;}
        public string patient_personal_id {get;set;}
    }
    

    Your call will look something like this:

    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
    }