MikeDub
MikeDub

Reputation: 5283

Making POST API request via c# with hashed API Key

I'm trying to make a POST call (from a C# WPF app) to a web service on an internal intranet (so I can't give the exact URL sorry), which is basically a url shortening service.

The page gives the following instructions:


In order to use the API service, simply make HTTP POST requests to this URL: https://...internalAddress.../api/<method>

For example, to create a snip, make an HTTP POST request to: https://...internalAddress.../api/shorten

with these parameters:

api_key hash of a registered API key

url URL to be shortened


Now I have tried to implement this in a couple of different ways with what I've found via google / here, these are:

1:

            string apiKey = "xxxx11112222333";
            string urlForShortening = @"http://www.codeproject.com/Tips/497123/How-to-make-REST-requests-with-Csharp";
            string destination = @"https://internalurl/api/shorten";

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(destination);
            httpWebRequest.ContentType = "text/json";
            httpWebRequest.Method = "POST";                

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write("{'api_key': '" + apiKey + "', 'url': '" + urlForShortening + "'}");
            }
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var responseText = streamReader.ReadToEnd();
                MessageBox.Show(responseText);
            }

2: (Using the rest library created in the article found in the shortening link)

            string apiKey = "xxxx11112222333";
            string urlForShortening = @"http://www.codeproject.com/Tips/497123/How-to-make-REST-requests-with-Csharp";
            string destination = @"https://internalurl/api/shorten";

            RestClient client = new RestClient(destination, HttpVerb.POST,
                "{'api_key': '" + apiKey + "', 'url': '" + urlForShortening + "'}");
            var json = client.MakeRequest();
            MessageBox.Show(json);

have also tried feeding in the jsonData in double quotes:

var jsonData = "{\"api_key\": \"" + apiKey + "\", \"url\": \"" + urlForShortening + "\"}";

The result from both methods I always get is:

{"status": 400, "message": "Missing API key"}

Can someone please shed some light on what I'm doing wrong?

From the brief, I think the key may need to be hashed in some form and not sure how to do this.

Upvotes: 1

Views: 6899

Answers (2)

MikeDub
MikeDub

Reputation: 5283

Turns out my whole implementation was wrong, I was trying to send the data as JSON / using the wrong classes instead of a vanilla HTTP POST.

I used Method 2 found in this article and it worked fine: HTTP request with post

ie.

            using (var client = new WebClient())
            {
                var values = new NameValueCollection();
                values["api_key"] = "xxxx11112222333";
                values["url"] = @"http://www.codeproject.com";
                string destination = @"https://internalurl/api/shorten";
                var response = client.UploadValues(destination, values);
                var responseString = Encoding.Default.GetString(response);
                MessageBox.Show(responseString);
            }

Thanks for your help though!

Upvotes: 2

sudheeshix
sudheeshix

Reputation: 1591

I assume that you are attempting to send json data in the request body for the POST call to the API.

You dont seem to be providing a valid json here though. This is what you are sending now:

{'api_key':'someApiKey'}
{'url':'someUrlForShortening'}

Use a json validator to ensure you have a valid json document before you attempt to send it to the API.

A valid json would be

{
  "api_key":"someApiKey",
  "url":"someUrlForShortening"
}

Upvotes: 1

Related Questions