I got a form where user will insert data and save it to the database. After save, it will do a post with data to the URL(RequestURL) given. This RequestURL will analyze the data and pass back the result to the different URL(ResponseURL). Below is the simple code for it.
public class RequestSender
{
private static HttpClient _client;
public RequestSender()
{
if (_client== null)
{
_client= new HttpClient();
_client.BaseAddress = new Uri("https://example.com/api/");
}
}
[HttpPost]
public async Task<string> SaveData()
{
// get data from the form
// save the data
// Save data is easy, already done it
// Here is the problem
// after save the data, it will send a post to the default URL
var response = await _client.PostAsync(path);
return "OK";
}
}
I'm using httpclient, but the problem is I only get the response of the post. What I want is, it will post data and redirect to the URL like an action attribute in the form below. So, the URL will handle the data and pass back to the ResponseURL. Appreciate your help.
<form action="https://example.com/api/" method="post">
Actually I try to integrate the payment gateway. Here is the flow:
Actually, this can be done by submitting another form from client side, but instead of doing that, i try to post within the first form (save data).
You can return RedirectResult
from action
[HttpPost]
public async Task<string> SaveData()
{
// some code
var response = await _client.PostAsync(path);
return Redirect("https://example.com/api/");
}
If you want HttpClient.PostAsync() with the data , you could try the below code :
[HttpPost]
public async Task<string> SaveData()
{
//Since the API will need the new data in JSON format
//therefore I am serializing the data into JSON and then converting it to a StringContent object
StringContent content=new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json" );
//The StringContent object is then added to the API request by adding it to the 2nd parameter of the PostAsync() method.
var response = await _client.PostAsync(path ,content);
var content= response.Content.ReadAsStringAsync().Result;
return "OK";
}
Reference : https://www.yogihosting.com/aspnet-core-consume-api/
This part you have it already 1- User submit a form, save to the database.
2/3- After form save success, will have another post from the backend to the URL (Payment site). Payment site will be handling the post data and send back to the ResponseURL.
This can be done in 2 different ways, you do that using an HttpClient as @Alexander suggested
var response = await _client.PostAsync(paymentUrl);
var responseModel = JsonConvert.DeserializeObject<ResponseModelDto>(json)
var returnUrl = responseModel.ReturnUrl
Or you need to do it from your front end, doing an async post to the PaymentPage and process the response (ResponseUrl) via javascript.
4 - Here I will read the response and handling it, based on the response. With the response, you can redirect you can do anything you need
var returnUrl = responseModel.ReturnUrl;
return Redirect(returnUrl);
But there are some integrations with payment websites, that you normally redirect the user passing parameters via a post or a get. The website handles the request as part of the querystring or as part of the body from your post, you should send as well a returnUrl , where they will send any information(Dto) back to be processed it from your side. But that will depend on your specific scenario. Hope this clarifies your doubts
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.