Paul Prichard
Paul Prichard

Reputation: 71

Cannot access azure storage table using SAS via c#. Works in postman and browser

I am trying to obtain and parse a table from azure storage.

I generated a Shared Access Signature in azure storage explorer.

If I paste the generated url into browser or postman I get the table back as xml.

However, trying to do a HttpWebRequest with the url results in

System.Net.WebException: 'The remote server returned an error: (415) Unsupported Media Type.'

I have tried different content and accept types

const string url = @"https://laptopdeploymentfiles.table.core.windows.net/PaulLoginScript?st=2019-08-21T08%3A10%3A22Z&se=2019-08-22T08%3A10%3A22Z&sp=r&sv=2018-03-28&tn=paulloginscript&sig=***";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.Method = "GET";

var webResponse = request.GetResponse();

I have tried both

request.Accept = "application/xml";

and

 request.ContentType = "application/xml";

but still get the same error.

I have also tried creating and accessing a new table with only simple data. The data accessed via the brower is valid xml, but I still get the same error from a c# app.

The solution was to use both the following contenttype and accept formats

  request.Accept = "application/json";
     request.ContentType = "application/json";

Upvotes: 4

Views: 706

Answers (3)

Paul Prichard
Paul Prichard

Reputation: 71

The solution was to use both the following contenttype and accept formats

request.Accept = "application/json";
request.ContentType = "application/json";

Upvotes: 2

Casey Crookston
Casey Crookston

Reputation: 13955

I think you need to tell your request what kind of data to expect. Try adding this:

request.ContentType = "application/xml";

I've not tested, so I can't promise this will work. Let me know if it does!

EDIT:

Paul, I tested this code below:

const string url = @" -- used my own xml file in Azure Blob Storage --";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/xml";
var webResponse = request.GetResponse();

It worked with no errors. webResponse was populated just fine. I just whipped up a console app and put these 5 lines of code in it, and then stepped through in debug mode to see what would happen.

I wonder what is different about your environment? Could you try isolating these lines of code?

EDIT 2:

Just a thought... are you sure the XML in your file is valid?

Upvotes: 0

rickvdbosch
rickvdbosch

Reputation: 15621

Tested your specific scenario and got it working by using the HttpClient class.

Here's the code I used to get it working:

// Create an instance of HttpClient with the BaseAddress
var client = new HttpClient
{
    BaseAddress = new Uri("https://<STORAGE_NAME>.table.core.windows.net/")
};

// Add an Accept Header to tell the service you're expecting the data in JSON format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

// Get the actual table
var result = await client.GetAsync("<REST_OF_THE_URI");

When omitting the Accept-header, this code gives the error message:

Atom format is not supported.

EDIT
Taken from Query Tables - Request headers about the Accept header you can specify:

Optional. Specifies the accepted content type of the response payload. Possible values are:

  • application/atom+xml (versions prior to 2015-12-11 only)
  • application/json;odata=nometadata
  • application/json;odata=minimalmetadata
  • application/json;odata=fullmetadata

Upvotes: 1

Related Questions