English 中文(简体)
REST Windows Phone Photo upload
原标题:


I m trying to upload a photo to a REST api in a Windows Phone 7 application using RestSharp for my Gets/Posts.
The post parameters are as follows:

photo: The photo, encoded as multipart/form-data
photo_album_id: Identifier of an existing photo album, which may be an event or group album

I ve created my request, but every time I get back "{"details":"missing photo parameter","problem":"The API request is malformed"}

My photo parameter looks like this:

"---------------------------8cd9bfbafb3ca00 Content-Disposition: form-data; name="filename"; filename="somefile.jpg" Content-Type: image/jpg (some binary junk listed here) -----------------------------8cd9bfbafb3ca00--"

I m not quite sure if it s a problem with how I m presenting the binary data for the image (currently in my PhotoTaskCompleted event, I read the contents of e.ChosenPhoto into a byte[] and pass that to a helper method to create the form data) or if I just don t create the form correctly.

I m just trying to do this a simple as possible, then I can refactor once I know how it all works.

 void ImageObtained(object sender, PhotoResult e)
    {

        var photo = ReadToEnd(e.ChosenPhoto);
        var form = PostForm(photo);
        var request = new RequestWrapper("photo", Method.POST);
        request.AddParameter("photo_album_id", _album.album_id);
        request.AddParameter("photo", form);

        request.Client.ExecuteAsync<object>(request, (response) =>
          {
               var s = response.Data;
          });
    }

    private string CreateBoundary()
    {
        return "---------------------------" + DateTime.Now.Ticks.ToString("x");

    }

    private string PostForm(byte[] data)
    {
        string boundary = CreateBoundary();
        StringBuilder post = new StringBuilder();
        post.Append(boundary);
        post.Append("
");
        post.Append("Content-Disposition: form-data; name="filename"; filename="somefile.jpg"");
        post.Append("
");
        post.Append("Content-Type: image/jpg");
        post.Append("

");
        post.Append(ConvertBytesToString(data));
        post.Append("
");
        post.Append("--");
        post.Append(boundary);
        post.Append("--");

        return post.ToString();
    }

    public static string ConvertBytesToString(byte[] bytes)
    {
        string output = String.Empty;
        MemoryStream stream = new MemoryStream(bytes);
        stream.Position = 0;
        using (StreamReader reader = new StreamReader(stream))
        {
            output = reader.ReadToEnd();
        }

        return output;
    }
最佳回答

Hammock for Windows Phone makes this real simple. You just add the file to the request using the AddFile method and pass it the photo stream.

        var request = new RestRequest("photo", WebMethod.Post);
        request.AddParameter("photo_album_id", _album.album_id);
        request.AddFile("photo", filename, e.ChosenPhoto);
问题回答

Hum are you sure that your PostForm is correct ? The content-* params should be set in the headers of your POST and not in the body ?

var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.Authorization,"blabla");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";




相关问题
Allow RESTful DELETE method in asp.net mvc?

im currently setting up asp.net to accept DELETE http verb in the application. However, when i send "DELETE /posts/delete/1" i always get a 405 Method not allow error. I tried to take a look at ...

Most appropriate API for URL shortening service

I ve just finished an online service for shortening URLs (in php5 with Zend Framework); you can enter an URL and you get an short URL (like tinyurl and such sites). I m thinking about the API for ...

Use HTTPClient or HttpUrlConnection? [closed]

We re implementing a REST client on JRE 1.4. Seems two good options for a client REST framework are HttpClient and HttpUrlConnection. Is there a reason to use HttpClient over the JRE s ...

Why can t I find the truststore for an SSL handshake?

I m using the Spring RESTTemplate on the client side to make calls to a REST endpoint. The client in this case is a Spring app and Tomcat is the servlet container. I m running into issues making a ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Three Step Buyonline The RESTful way

We are re-developing our buyonline functionality and we are doing it the RESTful way. The process is a three step one and the customer is asked to enter data at each step. Let s say the three URL s ...

热门标签