Search code examples
c#restwebrequest

Can not understand POST request Format


these are lines from this article https://developer.amazon.com/public/apis/experience/cloud-drive/content/nodes

I want to upload video using this explaianation

Upload File Uploads the file content along with its metadata information.

POST : {{contentUrl}}/nodes?suppress={suppress}

suppress : (Optional)

deduplication: disables checking for duplicates when uploading Body Parameters:

Multi-form part

--------- metadata ------------

name (required) : file name. Max to 256 Characters. kind (required) : "FILE" labels (optional) : Extra information which is indexed. For example the value can be "PHOTO" properties (optional) : List of properties to be added for the file. parents(optional) : List of parent Ids. If no parent folders are provided, the file will be placed in the default root folder. ---------content ------------

File Bytes

Sample Request:

POST /cdproxy/nodes?localId=testPhoto HTTP/1.1 Host: content-na.drive.amazonaws.com Authorization: Bearer Atza|IQEBLjAsAhReYeezFcFdYzqrFOJGv3EG

----WebKitFormBoundaryE19zNvXGzXaLvS5C Content-Disposition: form-data; name="metadata"

{"name":"fooo.jpg","kind":"FILE"} ----WebKitFormBoundaryE19zNvXGzXaLvS5C Content-Disposition: form-data; name="content"; filename="db5df4870e4e4b6cbf42727fd434701a.jpg" Content-Type: image/jpeg

----WebKitFormBoundaryE19zNvXGzXaLvS5C

cURL Request:

curl -v -X POST --form 'metadata={"name":"testVideo1","kind":"FILE"}' --form 'content=@sample_iTunes.mp4' 'https://content-na.drive.amazonaws.com/cdproxy/nodes?localId=testVideo1&suppress=deduplication' --header "Authorization: Bearer Atza|IQEBLjAsAhQ5zx7pKp9PCgCy6T1JkQjHHOEzpwIUQM"

I have written following code in c# //FileName 1: File from Computer //FileName 2:File to be named on amazon cloud.

   if (AccessCode == null)
        {
            return ;
        }
        WebRequest request =                 WebRequest.Create(EndPointUrl+"//+&suppress=deduplication'");
        request.Method = "POST";
        string code = "Bearer " + AccessCode;
        request.Headers.Add("Authorization", code);
        string postData =@"Content-Disposition: form-data; name=""metadata""   {""name"":"""+FileName1+@""",""kind"":""+FILE""}
        Content-Disposition: form-data; name=""content"";
        filename="""+FileName2+@"""
        Content-Type: video/wmv";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        WebResponse response = request.GetResponse();
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        Console.WriteLine(responseFromServer);
        reader.Close();
        dataStream.Close();
        response.Close();        

The problem is i can't translate it correctly. it is giving 404 error.

{"message":"Resource does not exist."}

Kindly help me to understand it.


Solution

  • its a normal multipart/form-data POST request with first parameter value is Stringified JSON object and second one is a image file.

    Please have a look at : Upload files with HTTPWebrequest (multipart/form-data)

    then you can

    NameValueCollection nvc = new NameValueCollection();
    nvc.Add("metadata", "{\"name\":\"fooo.jpg\",\"kind\":\"FILE\"}");
    HttpUploadFile("THE_URL_HERE", @"C:\test\fooo.jpg", "content", "image/jpeg", nvc);