This topic walks through how the sample Android client uploads an image to AuctionWorx Web API. A subsequent procedure pairs the uploaded bitmap with the rest of the listing's details.
On receiving the image, AuctionWorx stores the data and generates variations of the image (for example a thumbnail version). If all goes well, Web API returns a List of Media that it created. Each returned Media object includes the AuctionWorx unique identifier for the stored image.
In the UploadImageBytes() method (at the bottom of the page), line 4 calls the GetImageBytesAsync() method which converts the image data into a MemoryStream object.
Next, on line 7, the routine creates an instance of the HttpClient class (variable name client).
Lines 8 and 9 add the user's credentials to an Authorization header using the TryAddWithoutValidation() method. The format of the header's value is significant - the space character after RWX_BASIC and the colon between the username and password are required.
The image (in the variable named stream) becomes part of the Request's content payload, specifically as a MultipartFormDataContent object in lines 10 and 11.
Line 11 adds the stream as a file part to the multipart/form-data MIME type. In this example, the Add() method's second parameter (upload) is an name for the content. The third parameter (myuploadedimage) is a required filename.
Line 12 adds a second HTTP content part - a single string part with the name "context". AuctionWorx Web API rejects the upload if the name "context" is missing.
On lines 13 and 14, the HttpClient's PostAsync() method posts the image data and headers to the address in the first parameter (uri). The return is captured by the client as an HttpResponseMessage object into the variable name response.
To extract the contents of the returned data from the HttpResponseMessage, line 15 uses the ReadAsStringAsync() method. The string is a Json-formatted List of Media objects. The JsonConvert object's static DeserializeObject() method conveniently restores the Media objects in line 16.
Our demo client is most interested in the server's name for the uploaded image. This GUID is available as the Media object's GUID property (line 20) where it is returned to the caller for inclusion in a post of listing details.
public async Task<Guid> UploadImageBytes (System.Uri uri, string username, string password) { using (var stream = await GetImageBytesAsync (maxWidth: 400, maxHeight: 400)) { var client = new HttpClient(); client.DefaultRequestHeaders.TryAddWithoutValidation ("Authorization", "RWX_BASIC " + username + ":" + password); var content = new MultipartFormDataContent(); content.Add(new StreamContent(stream), "upload", "myuploadedimage"); content.Add(new StringContent("UploadListingImage"), "context"); HttpResponseMessage response = await client.PostAsync (uri, content, new CancellationToken()); var returnedcontent = await response.Content.ReadAsStringAsync(); List<Media> medias = JsonConvert.DeserializeObject<List<Media>>(returnedcontent); var media = medias.FirstOrDefault(); if (media == null) throw new Exception(); return media.GUID; } }
Copyright © 2002-2022. RainWorx Software. All rights reserved.