0

I have some data in my app's backend that I need to post from my application to another application.

I was thinking about creating form, filling it with the data and auto-posting with javascript within onLoad. But this seems somehow outdated practice for me. What would be the correct way to post from backend to some other application's url using ASP.NET 5 & MVC6 features?

Note: preferably, it should be JSON & RESTful design (controller will be accepting the data on another end), though I don't think this should change anything.

1 Answer 1

1

You should be able to use e.g. ordinary HttpClient. This is an example from the MS blog.

using System.Net.Http;
using (var client = new HttpClient())
{
    var baseUri = "http://playapi.azurewebsites.net/api/products";
    client.BaseAddress = new Uri(baseUri);
    client.DefaultRequestHeaders.Accept.Clear();
    var response = await client.GetAsync(baseUri);
    if (response.IsSuccessStatusCode)
    {
        var responseJson = await response.Content.ReadAsStringAsync();
        //do something with the response here. Typically use JSON.net to deserialise it and work with it
    }
}

This is a GET example, but POST should be pretty similar. If you control both servers, then you can use a fancy thing called Swagger (and Swashbuckle nuget package for .NET). It is kind of WSDL for the REST API, it can generate full proxy to access your API, similar to what WCF does + a nice page with documentation and testing forms.

P.S. Not sure of the state of Swashbuckle for ASP.NET Core, but the pre-release version is available on Nuget as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.