DEO KUMAR DAS
DEO KUMAR DAS

Reputation: 99

You must provide a request body if you set ContentLength>0 or SendChunked==true. Do this by calling [Begin]GetRequestStream before [Begin]GetResponse

I tried to upload a file on Oracle cloud infrastructure IAAS, but I'm getting this error:

You must provide a request body if you set ContentLength>0 or SendChunked==true. Do this by calling [Begin]GetRequestStream before [Begin]GetResponse

I am not sure whether the file that I attached in the body is in the correct format . The API signing is correct and I am in doubt only about whether the code that I wrote is correct. The code snippet is below:

    FileInfo f = new FileInfo(FileUpload1.FileName);
    byte[] filebyte =FileUpload1.FileBytes;

    var postdata = Encoding.UTF8.GetBytes(filebyte.ToString());
    Console.Write(postdata.Length);
  
    var tenancyId = ConfigurationManager.AppSettings["BMCTenancyId"];
    var userId = ConfigurationManager.AppSettings["BMCUserId"];
    var fingerprint = ConfigurationManager.AppSettings["BMCFingerprint"];
    var privateKeyPath = ConfigurationManager.AppSettings["BMCPrivateKeyPath"];
    var privateKeyPassphrase = ConfigurationManager.AppSettings["BMCPrivateKeyPassphrase"];
   
    var signer = new RequestSigner(tenancyId, userId, fingerprint, privateKeyPath, privateKeyPassphrase);

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    var uri = new Uri($"https://objectstorage.us-phoenix-1.oraclecloud.com/");
  
    var request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "POST";
    request.Accept = "application/json";
    request.SendChunked = true;
    request.ContentType = "text/plain";

    request.ContentLength =postdata.Length;
    try
    {
        using (var stream = request.GetRequestStream())
        {
            stream.Write(postdata, 0, postdata.Length);
            stream.Close();
        }
    }
    catch(Exception ex)
    {
        Response.Write(ex.Message);
    }

    request.Headers["x-content-sha256"] = Convert.ToBase64String(SHA256.Create().ComputeHash(postdata));

    signer.SignRequest(request);

    Console.WriteLine($"Authorization header: {request.Headers["authorization"]}");

    ExecuteRequest(request);

    Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}", request.ContentLength);
}

private static void ExecuteRequest(HttpWebRequest request)
{
    try
    {
        var webResponse = (HttpWebResponse)request.GetResponse();
        var response = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
        Console.WriteLine($"Response: {response}");
    }
    catch (WebException e)
    {
        Console.WriteLine($"Exception occurred: {e.Message}");
        Console.WriteLine($"Response: {new StreamReader(e.Response.GetResponseStream()).ReadToEnd()}");
    }
}

Upvotes: 0

Views: 5449

Answers (1)

user3291073
user3291073

Reputation: 62

For one thing, you'll need to update the URL to the following format:

var uri = new Uri($"https://objectstorage.us-phoenix-1.oraclecloud.com/n/{namespaceName}/b/{bucketName}/o/{objectName}");

Docs: https://docs.cloud.oracle.com/iaas/api/#/en/objectstorage/20160918/Object/PutObject

Also, can you please edit the question to include the complete error you are receiving, that will help with debugging.

Upvotes: -1

Related Questions