private static async Task <string> SendRequest(string url) { RequestJson requestJson = new RequestJson() { session = ApiHelper.SessionKey }; string jsonString = JsonConvert.SerializeObject(requestJson); var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); using (HttpResponseMessage response = await ApiHelper.ApiClient.PostAsync(url, content).ConfigureAwait(false)) { if (response.IsSuccessStatusCode) { string responseContent = await response.Content.ReadAsStringAsync(); return(responseContent); } else { throw new Exception(response.ReasonPhrase); } } }
private static async Task <string> UploadImage(string path) { string url = $"upload"; using (var content = new MultipartFormDataContent()) { //firstPart RequestJson fits = new RequestJson() { session = ApiHelper.SessionKey }; string jsonString = JsonConvert.SerializeObject(fits); var firstPart = new StringContent(jsonString, Encoding.UTF8, "application/json"); //secondPart byte[] img = null; try { img = ImgHelper.ImgToByteArray(path); } catch { Properties.Settings.Default.message = "File not found."; return(null); } content.Add(firstPart, "request-json"); content.Add(new StreamContent(new MemoryStream(img)), "file", "file.fit"); using (HttpResponseMessage response = await ApiHelper.ApiClient.PostAsync(url, content).ConfigureAwait(false)) { if (response.IsSuccessStatusCode) { string responseContent = await response.Content.ReadAsStringAsync(); return(responseContent); } else { throw new Exception(response.ReasonPhrase); } } } }