Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sonja 133 posts 621 karma points
    Jan 12, 2019 @ 11:03
    Sonja
    0

    making web api call from umbraco

    hi, I have a Umbraco web application that is supposed to make api call . When I do the api call from postman or from console application everything works perfectly. When I'm making the API call from umbraco I don't receive the response. I'm getting stuck on

      HttpResponseMessage response = await client.PostAsJsonAsync("api/user/login", request);
    

    The code is completely same in the console application and in the Umbraco application.

    Dud anyone had similar experience? Please advice how do I fix my problem and receive the response. Thanks

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 12, 2019 @ 22:20
    Alex Skrypnyk
    0

    Hi Sonja

    What is full request URL you are trying to use?

    Alex

  • Sonja 133 posts 621 karma points
    Jan 13, 2019 @ 09:43
    Sonja
    0

    Hi Alex I'm setting the request url previously with

    client.BaseAddress = new Uri(ConfigurationManager.AppSettings["myUrl"]);
    

    and it is outside url, our service provider url. The web api is not in Umbraco, not in our domain but totally outside

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 13, 2019 @ 22:41
    Alex Skrypnyk
    0

    Hi Sonja

    Where in Umbraco do you run this code? Is it a controller or some service? event?

    Maybe there is an issue with async, that isn't working properly.

    Thanks,

    Alex

  • Sonja 133 posts 621 karma points
    Jan 14, 2019 @ 05:07
    Sonja
    0

    Hi Alex,

    I'm running the code when a certain page is loaded, when I'm triggering GET of an MVC Controller. It is not inside the controller class but in another class. Is Umbraco blocking something and not allowing this kind of code to run? Please advice how do I proceed

    Thanks,

    Sonja

  • Alexander Croner 71 posts 282 karma points
    Jan 14, 2019 @ 08:04
    Alexander Croner
    0

    I think you have to show more of the code in order for anyone to help you.

  • Sonja 133 posts 621 karma points
    Jan 14, 2019 @ 08:26
    Sonja
    0

    This is how I'm sending the login request

    public static async Task<LoginResponse> LoginMyUser(LoginRequest request)
            {
                var loginResponse = new LoginResponse();
                client.BaseAddress = new Uri(ConfigurationManager.AppSettings["DGUrl"]);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.PostAsJsonAsync("api/user/login", request);
                response.EnsureSuccessStatusCode();
                if (response.IsSuccessStatusCode)
                {
                    var dataObject = response.Content.ReadAsAsync<LoginResponse>().Result;
                    loginResponse.token = dataObject.token;
                    loginResponse.sessionName = dataObject.sessionName;
                    loginResponse.sessionId = dataObject.sessionId;
    
                }
    
                loginDgResponse.response = response;
    
                return  loginDgResponse;
            }
    

    This is how I'm sending it

       var loginRequest = new LoginRequest()
                        {
                            username = ConfigurationManager.AppSettings["dusername"],
                            password = ConfigurationManager.AppSettings["dpassword"]
                        };
                        var getResponse = LoginUser.LoginMyUser(loginRequest).Result; 
    

    The code works perfectly in console application so it is not the code, it is some setting because I never received any response.

  • Sonja 133 posts 621 karma points
    Jan 14, 2019 @ 12:34
    Sonja
    0

    I also did a simple MVC application and I got same results. I don't think it is Umbraco but something with IIS or IISExpress maybe

  • Sonja 133 posts 621 karma points
    Jan 16, 2019 @ 05:05
    Sonja
    0

    Guys, I still have this problem. Even if it might be MVC related, did nobody ever had such problem and what might be a solution?

  • Tor Langlo 190 posts 533 karma points
    Jan 16, 2019 @ 06:01
    Tor Langlo
    0

    If you monitor the request and response with Chrome Developer Tools’ Network tab, or similar, what do you see? What kind of response? Or timeout?

    -Tor

  • Sonja 133 posts 621 karma points
    Jan 16, 2019 @ 11:09
    Sonja
    0

    in chrome I'm not seeing the api call. I only see the umbraco get page and it is returning me 302

  • Tor Langlo 190 posts 533 karma points
    Jan 16, 2019 @ 18:24
    Tor Langlo
    1

    It might have something to do with how your configuring your HttpClient instance, so more information (code) would be helpful. I see you're using PostAsJsonAsync method, maybe you could replace that with your own code, making it synchronous and doing the json serialization yourself. This just to nail down the issue, once you have found the problem you might put the original code back in.

    Another thing you could do to see the network traffic is to use Fiddler or a similar tool. My suggestion to use Chrome Dev Tools will not work since the API call is not from the web client, but from your server.

    /Tor

  • Sonja 133 posts 621 karma points
    Jan 18, 2019 @ 12:46
    Sonja
    0

    Hi Tor,

    I have tried with Fiddler, and I got Tunnel to the service provider, the result is 200, the response is empty, and the request I can't find. From the response I'm finding

    HTTP/1.1 200 Connection Established

    FiddlerGateway: Direct

    StartTime: 16:31:16.324

    Connection: close

    EndTime: 16:31:16.385

    ClientToServerBytes: 130

    ServerToClientBytes: 7

    Process:iisexpress:1224

    Did I send it correctly? Is the problem with the service provider? Is there some testing web api so I can try to send request there to see if it is going dorrectly from my side and it is not the service provider restriction? I asked the guys from the service provider and they said they don't have any restrictions.

    Please advice

  • Tor Langlo 190 posts 533 karma points
    Jan 20, 2019 @ 02:38
    Tor Langlo
    1

    If you could find your request in Fiddler, and then compare it to the success calls from postman or the console app you mentioned earlier, then that would be helpful.

    -Tor

  • Sonja 133 posts 621 karma points
    Jan 22, 2019 @ 07:07
    Sonja
    0

    Actually I have compared the both calls and both calls are OK. So this looks like the problem is when I receive the response. both calls have good request and response but my response is not received

  • Tor Langlo 190 posts 533 karma points
    Jan 22, 2019 @ 16:15
    Tor Langlo
    0

    So to be clear,

    1. You see the json response in Fiddler?

    2. You get stuck on the following line in your code?

    HttpResponseMessage response = await client.PostAsJsonAsync("api/user/login", request);
    
    1. Not sure if it's relevant, but the these two lines can be improved:
    response.EnsureSuccessStatusCode();
    if (response.IsSuccessStatusCode)
    

    since the call to response.EnsureSuccessStatusCode() will throw an exception if response.IsSuccessStatusCode is false. In other words, you don't need the if statement.

    -Tor

  • Sonja 133 posts 621 karma points
    Jan 23, 2019 @ 12:44
    Sonja
    0

    Hi Tor,

    1. yes

    2. yes

    3. Thanks I will fix this

  • Tor Langlo 190 posts 533 karma points
    Jan 23, 2019 @ 19:14
    Tor Langlo
    1

    So is the problem then narrowed down to either a problem with how the HttpClient is set up, or an issue with the underlying Umbraco web site?

    Could you do a test using the same HttpClient set up against a different API and/or service and test it?

    -Tor

  • Sonja 133 posts 621 karma points
    Jan 28, 2019 @ 07:27
    Sonja
    0

    Hi Tor,

    I have tried to send post api call to a local web api, sent empty call and received the response but only when I remove await and add Result at the end.

    HttpResponseMessage response = client.PostAsJsonAsync("api/user/login", request).Result;
    

    But it was api call hosted on local.

    I did the same in a new empty MVC application without Umbraco and I received my api response perfectly.

    However when I do this in my umbraco application for the api call that worked in MVC application without Umbraco in Umbraco I'm getting error ""One or more errors occurred."" and InnerException {"The request was aborted: Could not create SSL/TLS secure channel."}

  • Sonja 133 posts 621 karma points
    Jan 28, 2019 @ 11:08
    Sonja
    101

    when I added in Global.asax.cs the line ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; started working but without await

  • Tor Langlo 190 posts 533 karma points
    Jan 28, 2019 @ 16:52
    Tor Langlo
    0

    Good to hear you're getting further here! I'm probably a bit outside of my comfort zone now, but maybe there's something in your code after the the await call (i.e. in the call stack) which causes the request to complete or abort before the async call completes (and continues with the lines after the await)?

    Are you using await calls to the top of the call stack?

    -Tor

  • Sonja 133 posts 621 karma points
    Jan 29, 2019 @ 11:20
    Sonja
    0

    Hi Tor,

    When I'm using await calls my call doesn't receive the response. So I have removed await almost everywhere in the code

    -Sonja

Please Sign in or register to post replies

Write your reply to:

Draft