2

Im trying to create a very simple c# console application to post some data to a web api. However whatever I do I get an error on the response like:

responseText "{\"info\":{\"status\":\"failed\",\"error\":{\"code\":1000,\"message\":\"Invalid argument from request\"}}}" string

The api http://www.detrack.com/api-documentation/ is looking for a post like

https://app.detrack.com/api/v1/deliveries/view/all.json?key=dab13cc0094620102d89f06c0e464b7de0782bb979258d3a&json={"date":"2014-08-29"}

I know using this in chrome advanced rest application extension returns a valid result. But When I try the same via this console code. I get an error!.

Here is the code I have in my console application.

using System;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://app.detrack.com/api/v1/deliveries/view/all.json?key=dab13cc0094620102d89f06c0e464b7de0782bb979258d3a&");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "json={\"date\":\"2014-08-28\"}";
            Console.WriteLine(json);
            streamWriter.Write(json);
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
            Console.WriteLine(responseText);
            Console.ReadKey();

        }


    }
}
}

Any help/guidance would be really appreciated

brendan

Onel Sarmiento
  • 1,608
  • 3
  • 20
  • 46
user1517598
  • 111
  • 1
  • 4
  • 11

2 Answers2

0

So I'm looking at this:

string json = "json={\"date\":\"2014-08-28\"}";

And according to the brief description on detrack that is not what you want. They're expecting valid json and that isn't. Here's what you should be considering valid json:

string json = "{\"date\":\"2014-08-28\"}";

Be warned I don't know about your escaping of quotes. I would serialize that differently; either a strongly typed class or an anonymous class. Anon would look like this:

string json = JsonConvert.DeserializeObject(new { date = "2014-08-28" });

Setting aside any concerns about time, timezones, utc, etc, that will serialize your structures correctly. Here's a scratchy program from linqpad:

void Main()
{
    var json = Newtonsoft.Json.JsonConvert.SerializeObject(new { date = "2014-08-28"});
    Console.WriteLine(json);
}

>>> {"date":"2014-08-28"}
clarkitect
  • 1,720
  • 14
  • 23
  • Hi Jeff,thanks for the feedback. I have tried using your options for json serializing and im still hitting a roadblock. Ill keep plugging away. – user1517598 Sep 10 '14 at 21:14
0

You can try the (untested!) code below.

using System;
using System.Net;
using System.IO;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {

            var webAddr = "https://app.detrack.com/api/v1/deliveries/create.json";

            var httpWebRequest = (HttpWebRequest) WebRequest.Create(webAddr);

            httpWebRequest.ContentType = "application/x-www-form-urlencoded";

            httpWebRequest.Method = "POST";

            string postData = "key=dab13cc0094620102d89f06c0e464b7de0782bb979258d3a&json={""date"":""2014-08-28""}";

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            httpWebRequest.ContentLength = byteArray.Length;

            using(var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))

            {

                streamWriter.Write(byteArray, 0, byteArray.Length);

                streamWriter.Flush();

            }

            var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();

            using(var streamReader = new StreamReader(httpResponse.GetResponseStream()))

            {

                var result = streamReader.ReadToEnd();

                MessageBox.Show(result);

            }
        }
    }
}
  • Hi Dason, Thanks for posting your code. I have tried, but even with some small edits I can get it to post, but the response I get doesnt include any valid json data. The response I see is ` { "info":{ "status":"ok" },"results":[] } ` – user1517598 Sep 10 '14 at 21:21
  • Do you have a solution if instead of JSON, I want to refer the SFTP URL where a csv file is stored? PLease refer my question "http://stackoverflow.com/questions/35308945/accessing-sftp-url-from-console-application-using-c-sharp" – Yash Saraiya Feb 10 '16 at 09:35