1

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.

MikeDub
  • 5,143
  • 3
  • 27
  • 44

2 Answers2

2

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!

Community
  • 1
  • 1
MikeDub
  • 5,143
  • 3
  • 27
  • 44
1

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"
}
sudheeshix
  • 1,541
  • 2
  • 17
  • 28
  • Doesn't my 2nd method request it properly? – MikeDub Jan 27 '15 at 08:18
  • Can you use double-quotes instead? The 2nd method uses single quotes, which is not valid json either. – sudheeshix Jan 27 '15 at 08:20
  • Tried this as well...I've updated the question with how I've done it. – MikeDub Jan 27 '15 at 08:40
  • Now that you are sending a valid json in the request, and made sure that the URL is also fine, the only other thing you can check for (IMHO) is the HTTP response. Are you getting an HTTP 400 or 200? The response content you get appears to be a custom message sent by the services. So, you would have to contact the service provider to figure out what the issue is. Being the service provider, I usually try to provide enough info in the response to let the service consumer know what went wrong. For instance, it would help you better if the service returned "Missing API key. Expected: apiKey." – sudheeshix Jan 27 '15 at 18:25