.net core - rest-api


Because I did not find any documentation on how to go about creating a Microsoft .net core REST API client for Omeka, I figured out one myself. I’d like to discuss best practices on this Topic. Maybe other (.net) developers like to share their knowledge too.

Below you will find some C# examples.

Happy coding!
Peter Verberne

To Read:

Create a static http client:

 private static readonly HttpClient client = new HttpClient();

Call the client:

public async Task<string> GetItems(string path)
    UriBuilder uri= new UriBuilder(baseuri);
    uri.Path += HttpUtility.UrlDecode(path);
    HttpResponseMessage response = await client.GetAsync(uri.ToString());
    return await response.Content.ReadAsStringAsync();

To Create an Item including upload a file:

public async Task<string> PostImage(string jsonFile, string fileName)
    UriBuilder uri = new UriBuilder(baseuri);

   uri.Path += HttpUtility.UrlDecode("/items");
   uri.Query += "key_identity=" + key_id;
   uri.Query += "&key_credential="+ key_cred;
   string data = File.ReadAllText(jsonFile).ToString();
   byte[] uploadFile = File.ReadAllBytes(fileName);
   StreamContent file = new StreamContent(new MemoryStream(uploadFile));
   MultipartFormDataContent formContent = new MultipartFormDataContent();
   formContent.Add(new StringContent(data, Encoding.UTF8), "data");
   formContent.Add(file, "file[0]",fileName);
   string uristring = uri.ToString();
   HttpResponseMessage response=await client.PostAsync(uri.ToString(), formContent);
   return await response.Content.ReadAsStringAsync();

can you please provide full working code.

This topic was automatically closed 250 days after the last reply. New replies are no longer allowed.