简体   繁体   中英

Using RestSharp to upload an image via Etsy API

I'm trying to add an image along with a new listing to my Etsy account through a web application that I'm developing. Currently, I am using:

RestRequest request = new RestRequest("listings", Method.POST);

request.AddFile("image", ReadToEnd(o.mainPhoto), "test.bmp", "image/bmp");
request.AddParameter("image", "test.bmp");

request.AddParameter("title", "This is a test");
request.AddParameter("description", "Test Description");
request.AddParameter("status", "draft");
request.AddParameter("quantity", "1");    
request.AddParameter("price", "5");
request.AddParameter("is_supply", "false");
request.AddParameter("category_id", "68887420");
request.AddParameter("when_made", "2013");
request.AddParameter("who_made", "i_did");
request.AddParameter("shipping_template_id", 5463224); 
var etsyResponse = restClient.Execute<EtsyListing>(request);

The listing is created correctly except there is no image.

I noticed that the etsyResponse has content containing information regarding the image uploaded (ie size, name, etc..) including a "tmp_name" created for the image. Should I be associating the listing with the "tmp_name" from the etsyResponse instead of the uploaded name of the file?

Any help is appreciated.

I actually just had the exact same problem. I'm using a C# server side handler with RestSharp POSTing a new listing with image data from an HttpPostedFile instance. The draft listings were created just fine, but without any image. Like you mentioned, I saw the tmp_name and errors values which seemed to suggest the image was created. After experimenting, my final solution which has worked just fine is to:

  1. Create the new listing without the image file.
  2. Grab the new listing's listing_id value from the response.
  3. POST a second call to the resource - ("listings/[YOUR NEW LISTING'S ID]/images"). Add the image file to this request. I also add the rank parameter to control the image's display sequence.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM