3

I am trying to make a call to an api that interns calls an external api for data. The code I have written is:

    [HttpPost]
    public IHttpActionResult Post()
    {

        string _endpoint = "https://someurl.com/api/v1/models?auth_token=mytoken";
        var httpContext = (System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"];
        string upload_id = httpContext.Request.Form["upload_id"];
        string filename =  httpContext.Request.Form["filename"];
        string filesize = "1000";


        //return this.Ok<string>(upload_id + " " + filename);         


        var content = new FormUrlEncodedContent(new[] 
        {
            new KeyValuePair<string, string>("upload_id", upload_id),
            new KeyValuePair<string, string>("filename", filename),
            new KeyValuePair<string, string>("filesize", filesize)
        });  

        using (var httpClient = new HttpClient())
        {
            var response = httpClient.PostAsync(_endpoint, content).Result;
            return Json(JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result));
        }



    }

Client side I am then making a call via ajax to get the data:

$.ajax({
        url: '/api/tws',
        type: 'POST',
        data: { 'file': "EX-IGES.IGS", 'upload_id': "eb550576d2" },
        success: function (response) { 
                 console.log('response',response);
                 }
 });

However it is always returning null. I have verified API call works and everything is correct. I a little new to C#.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
w3bMak3r
  • 882
  • 8
  • 13
  • Why do you use `PostAsync` if you have to wait for the result? – John Saunders Dec 17 '14 at 01:39
  • I am not sure... I modeled it after code I found elsewhere in the API. How would you recommend I go. Any ideas or suggestions would be greatly appreciated. – w3bMak3r Dec 17 '14 at 01:45
  • 1
    @JohnSanders `HttpClient` only provides `...Async()` methods for HTTP methods – Nick Strupat Dec 17 '14 at 01:50
  • 1
    Try splitting your code into explicit statements and walk through with the debugger to see which call is not returning what you expect it to. – Nick Strupat Dec 17 '14 at 01:57
  • @NickStrupat Any ideas on a direction I might look in to fix this. I have been trying to integrate await but am a little lost. I really aprechiate the help guys. – w3bMak3r Dec 17 '14 at 01:58
  • @NickStrupat I am actually using dlls that I decompiled edited and recompiled so I do not have the ability to run through the debugger. I have added return strings after almost every line and they all seem to excute expect for the one that does the call (httpClient.PostAsync) returns null – w3bMak3r Dec 17 '14 at 02:00
  • 1
    Strange... Should be failing due to timeout (you deadlocking your request thread with `.Result`). please use Fiddler to check what server actually returns. – Alexei Levenkov Dec 17 '14 at 02:00
  • 1
    Try to add a breakpoint on this line: var response = httpClient.PostAsync(_endpoint, content).Result; After that in the immediate window, type this: httpClient.PostAsync(_endpoint, content) – User2012384 Dec 17 '14 at 02:01
  • @User2012384 The code is in a compiled dll. I can not add breakpoints that I know of? – w3bMak3r Dec 17 '14 at 02:06
  • 1
    Just copy the function to your form and use that function for debug.. – User2012384 Dec 17 '14 at 02:12
  • @AlexeiLevenkov By running this fiddler I am seeing that one of the fields is missing. Thank you so much... If you want to post an anser I will mark it. If you look in the ajax call I am passing the parameter "file" and in the c# code I am looking for "filename". Thank you! Thanks everyone! – w3bMak3r Dec 17 '14 at 02:13
  • 1
    @w3bMak3r Side note: I'd strongly recommend you to read on `async`/`await` to avoid problems with calling xxxAsync methods. Good answer/links - http://stackoverflow.com/questions/13140523/await-vs-task-wait-deadlock – Alexei Levenkov Dec 17 '14 at 03:18

2 Answers2

2

Break the code up so you can see what The Task<T> object returned from PostAsync is saying.

var responseTask = httpClient.PostAsync(_endpoint, content);
var response = responseTask.Result;
// At this point you can query the properties of 'responseTask' to look for exceptions, etc.
Nick Strupat
  • 4,928
  • 4
  • 44
  • 56
  • Thanks man.. I actually found it out using fiddler. I was passing in the wrong field label. – w3bMak3r Dec 17 '14 at 02:14
  • D'oh! We all overlooked that simple possibility. – Nick Strupat Dec 17 '14 at 02:17
  • Yeah.. How do you think I feel... I have been looking at this all day. I am a master at PHP and Javascript so just assumed it was my newness to C# that was blocking things. – w3bMak3r Dec 17 '14 at 02:20
  • I am just waiting to see if someone posts an answer pointing out the difference of "file" or "filename" if not I will mark this one as correct – w3bMak3r Dec 17 '14 at 02:21
2

Look at the ajax call you are passing in the parameter "File" but in the C# you are looking for the "Filename"

Fixed ajax code:

$.ajax({ url: '/api/tws', 
       type: 'POST', 
       data: { 'filename': "EX-IGES.IGS", 'upload_id': "eb550576d2" }, 
       success: function (response) { console.log('response',response); } 
 });
user2502479
  • 141
  • 1
  • 8