1

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();
        }
Baldie47
  • 1,148
  • 5
  • 16
  • 45
  • Are you having problems de-serializing the response? Or are you having problems serializing your data for your `POST` request? – Rahul Sharma Mar 14 '22 at 16:05
  • I don't entirely understand how should I send the data to the post, how to serialize it into that "payload" object – Baldie47 Mar 14 '22 at 16:38
  • What parameters do you require to `POST` to your `API`? Can you show us that `Model` or structure that you want to post? – Rahul Sharma Mar 14 '22 at 16:39
  • the information that I have is from the url for booking example, the model I want to post is based on the example call provided, I have "caregiver_id", "patient_personal_id", and then that PAYLOAD section, which I don't know if I should model entirely as an object or is something I'm not getting there. (my issue comes from setting up the POST object model with that Payload – Baldie47 Mar 14 '22 at 16:49
  • calling that url POST a booking, so I need to understand how to set a POST call from that url – Baldie47 Mar 14 '22 at 16:49
  • Did you get this working @Baldie47? – Rahul Sharma Mar 16 '22 at 03:57
  • @RahulSharma no :( I'm still trying to get it to work, I can do the GET calls, but the POST is failing, I don't know how to set the Payload – Baldie47 Mar 16 '22 at 08:18
  • Ok, Can you post more code on your attempt? Did you try doing it as I explained in my answer @Baldie47 – Rahul Sharma Mar 16 '22 at 08:28
  • yes, I'll add an edit – Baldie47 Mar 16 '22 at 08:28
  • What error are you getting on this line: `var response = _http.PostAsync(uri, content).Result;` ? @Baldie47 – Rahul Sharma Mar 16 '22 at 09:22
  • Can you post the endpoint method `api-bookings`. I think there is a mismatch in the payload request that you are sending and what the method is expecting @Baldie47 – Rahul Sharma Mar 17 '22 at 05:44
  • Yes. I was able to advance some. Please check here https://stackoverflow.com/questions/71498656/json-self-referencing-loop-on-post-call/71499635?noredirect=1#comment126374262_71499635 – Baldie47 Mar 17 '22 at 06:16

1 Answers1

1

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
}
Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54