Pass Composite Object to a Post Web API Method - api

I have the following composite object which I want to pass in web API post method in Xamarin:
public class ShoppingCartCustomizedItems
{
public AddToCart addToCart { get; set; }
public List<AddCustomizedProductSelectionsToCart> AddCustomizedProductSelectionsToCart { get; set; }
}
Below is the API service method to pass the data to the web API:
public static async Task<bool> AddCustomizedItemsInCart(ShoppingCartCustomizedItems addToCart)
{
var httpClient = new HttpClient();
var json = JsonConvert.SerializeObject(addToCart);
var content = new StringContent(json, Encoding.UTF8, "application/json");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", Preferences.Get("accessToken", string.Empty));
var response = await httpClient.PostAsync(AppSettings.ApiUrl + "api/ShoppingCartItems/addCustomizedShoppingCartItem", content);
if (!response.IsSuccessStatusCode) return false;
return true;
}
Finally the web API post method has the following signature:
[HttpPost("[action]")]
public IActionResult addCustomizedShoppingCartItem([FromBody] ShoppingCartCustomizedItem shoppingCartCustomizedItem)
Now whenever I send the post method from Xamarin, the shoppingCartCustomizedItem is always null. How can I address this and what is the best practice to pass composite object in web API method?

Related

Can I get data from web api in .net core with another api or api key?

I wrote a controller. I wrote it according to the web api I will use here. But how should I make my own created api?
Do I need to have my own created api where I write with HttpPost? I might be wrong as I am new to this.
public class GoldPriceDetailController : Controller
{
string Baseurl = "https://apigw.bank.com.tr:8003/";
public async Task<ActionResult> GetGoldPrice()
{
List<GoldPrice> goldPriceList = new List<GoldPrice>();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetDepartments using HttpClient
HttpResponseMessage Res = await client.GetAsync("getGoldPrices");
Console.WriteLine(Res.Content);
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
var ObjResponse = Res.Content.ReadAsStringAsync().Result;
goldPriceList = JsonConvert.DeserializeObject<List<GoldPrice>>(ObjResponse);
}
//returning the student list to view
return View(goldPriceList);
}
}
[HttpPost]
public async Task<IActionResult> GetReservation(int id)
{
GoldPrice reservation = new GoldPrice();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("https://apigw.bank.com.tr:8443/getGoldPrices" + id))
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string apiResponse = await response.Content.ReadAsStringAsync();
reservation = JsonConvert.DeserializeObject<GoldPrice>(apiResponse);
}
else
ViewBag.StatusCode = response.StatusCode;
}
}
return View(reservation);
}
}
Basically you need these steps:
HttpClient for local API.
HttpClient for external API.
Local API controller.
Inject external client into the local API.
Then inject the local API client into the razor page/controller.
HttpClient for Local API
public class LocalApiClient : ILocalHttpClient
{
private readonly HttpClient _client;
public LocalAPiClient(HttpClient client)
{
_client = client;
_client.BaseAddress(new Uri("https://localapi.com"));
}
[HttpGet]
public async Task<string> GetGoldPrices(int id)
{
// logic to get prices from local api
var response = await _client.GetAsync($"GetGoldPrices?id={id}");
// deserialize or other logic
}
}
HttpClient for External API
public class ExternalApiClient : IExternalHttpClient
{
private readonly HttpClient _client;
public ExternalAPiClient(HttpClient client)
{
_client = client;
_client.BaseAddress(new Uri("https://externalApi.com"));
// ...
}
[HttpGet]
public async Task<string> GetGoldPrices(int id)
{
// logic to get prices from external api
var response = await _client.GetAsync("getGoldPrices?id=" + id))
}
}
Register your clients in startup
services.AddHttpClient<ILocalHttpClient, LocalHttpClient>();
services.AddHttpClient<IExternalHttpClient, ExternalHttpClient>();
Create Local API Controller
and inject the external http client into it
[ApiController]
public class LocalAPIController : Controller
{
private readonly IExternalHttpClient _externalClient;
public LocalAPIController(IExternalHttpClient externalClient)
{
_externalClient = externalClient;
}
[HttpGet]
public async Task<string> GetGoldPrices(int id)
{
var resoponse = await _externalClient.GetGoldPrices(id);
// ...
}
}
Inject the local client into razor page/controller
public class HomeController : Controller
{
private readonly ILocalHttpClient _localClient;
public HomeController(ILocalHttpClient localClient)
{
_localClient = localClient;
}
public async Task<IActionResult> Index(int id)
{
var response = await _localClient.GetGoldPrices(id);
// ...
}
}

action overload in identityserver 4 api project

i woudl add 2 api into the controller of net core 3.1 api project:
first one accept access_token as parameter, the other one, called whitout parameters, retrives token from headers.
this the controller:
[Route("entitlements")]
public class EntitlementsController : ControllerBase
{
[Route("entitlements/get/{access_token}")]
public async Task<IActionResult> Get([FromQuery] string access_token)
{
EntitlementsResult oResult = GetEntitlements(access_token);
return new JsonResult(oResult);
}
[Route("entitlements")]
public async Task<IActionResult> Get()
{
var access_token = await GetAccessTokenFromHeaders();
EntitlementsResult oResult = GetEntitlements(token);
return new JsonResult(oResult);
}
}
but i get 404 not found calling the action from a client with this code:
public async Task<IActionResult> GetEntitlements()
{
var accessToken = await HttpContext.GetTokenAsync("access_token");
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
string apiUrl = Startup.StaticConfig.GetValue<string>("IdentityServer:api");
var content = await client.GetStringAsync($"{apiUrl}/entitlements");
ViewBag.Json = content;
return View("json");
}
For both of the action and controller contains [Route("entitlements")] attribute,so the correct url should be:{apiUrl}/[ControllerRoute]/[ActionRoute],change like below:
var content = await client.GetStringAsync($"{apiUrl}/entitlements/entitlements");

Azure Devops Oauth authentication: Cannot get access token (BadRequest Failed to deserialize the JsonWebToken object)

I'm trying to implement an OAUth 2.0 flow for custom webapplication for Azure Devops. I'm following this https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops documentation as well as this https://github.com/microsoft/azure-devops-auth-samples/tree/master/OAuthWebSample OauthWebSample but using ASP.NET Core (I also read one issue on SO that looked similar but is not: Access Azure DevOps REST API with oAuth)
Reproduction
I have registered an azdo app at https://app.vsaex.visualstudio.com/app/register and the authorize step seems to work fine, i.e. the user can authorize the app and the redirect to my app returns something that looks like a valid jwt token:
header: {
"typ": "JWT",
"alg": "RS256",
"x5t": "oOvcz5M_7p-HjIKlFXz93u_V0Zo"
}
payload: {
"aui": "b3426a71-1c05-497c-ab76-259161dbcb9e",
"nameid": "7e8ce1ba-1e70-4c21-9b51-35f91deb6d14",
"scp": "vso.identity vso.work_write vso.authorization_grant",
"iss": "app.vstoken.visualstudio.com",
"aud": "app.vstoken.visualstudio.com",
"nbf": 1587294992,
"exp": 1587295892
}
The next step is to get an access token which fails with a BadReqest: invalid_client, Failed to deserialize the JsonWebToken object.
Here is the full example:
public class Config
{
public string ClientId { get; set; } = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
public string Secret { get; set; } = "....";
public string Scope { get; set; } = "vso.identity vso.work_write";
public string RedirectUri { get; set; } = "https://....ngrok.io/azdoaccount/callback";
}
/// <summary>
/// Create azdo application at https://app.vsaex.visualstudio.com/
/// Use configured values in above 'Config' (using ngrok to have a public url that proxies to localhost)
/// navigating to localhost:5001/azdoaccount/signin
/// => redirect to https://app.vssps.visualstudio.com/oauth2/authorize and let user authorize (seems to work)
/// => redirect back to localhost:5001/azdoaccount/callback with auth code
/// => post to https://app.vssps.visualstudio.com/oauth2/token => BadReqest: invalid_client, Failed to deserialize the JsonWebToken object
/// </summary>
[Route("[controller]/[action]")]
public class AzdoAccountController : Controller
{
private readonly Config config = new Config();
[HttpGet]
public ActionResult SignIn()
{
Guid state = Guid.NewGuid();
UriBuilder uriBuilder = new UriBuilder("https://app.vssps.visualstudio.com/oauth2/authorize");
NameValueCollection queryParams = HttpUtility.ParseQueryString(uriBuilder.Query ?? string.Empty);
queryParams["client_id"] = config.ClientId;
queryParams["response_type"] = "Assertion";
queryParams["state"] = state.ToString();
queryParams["scope"] = config.Scope;
queryParams["redirect_uri"] = config.RedirectUri;
uriBuilder.Query = queryParams.ToString();
return Redirect(uriBuilder.ToString());
}
[HttpGet]
public async Task<ActionResult> Callback(string code, Guid state)
{
string token = await GetAccessToken(code, state);
return Ok();
}
public async Task<string> GetAccessToken(string code, Guid state)
{
Dictionary<string, string> form = new Dictionary<string, string>()
{
{ "client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" },
{ "client_assertion", config.Secret },
{ "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer" },
{ "assertion", code },
{ "redirect_uri", config.RedirectUri }
};
HttpClient httpClient = new HttpClient();
HttpResponseMessage responseMessage = await httpClient.PostAsync(
"https://app.vssps.visualstudio.com/oauth2/token",
new FormUrlEncodedContent(form)
);
if (responseMessage.IsSuccessStatusCode) // is always false for me
{
string body = await responseMessage.Content.ReadAsStringAsync();
// TODO parse body and return access token
return "";
}
else
{
// Bad Request ({"Error":"invalid_client","ErrorDescription":"Failed to deserialize the JsonWebToken object."})
string content = await responseMessage.Content.ReadAsStringAsync();
throw new Exception($"{responseMessage.ReasonPhrase} {(string.IsNullOrEmpty(content) ? "" : $"({content})")}");
}
}
}
When asking for access tokens the Client Secret and not the App Secret must be provided for the client_assertion parameter:

Correct way to return HttpResponseMessage as IActionResult in .Net Core 2.2

In .Net Core 2.2. I am creating a API Controller that routes the request to another Http endpoint based on payload.
[Route("api/v1")]
public class RoutesController : Controller
{
[HttpPost]
[Route("routes")]
public async Task<IActionResult> Routes([FromBody]JObject request)
{
var httpClient = new HttpClient();
// here based on request httpCLient will make `POST` or `GET` or `PUT` request
// and returns `Task<HttpResponseMessage>`. Lets assume its making `GET`
// call
Task<HttpResponseMessage> response = await
httpClient.GetAsync(request["resource"]);
/* ??? what is the correct way to return response as `IActionResult`*/
}
}
based on SO post i can do this
return StatusCode((int)response.StatusCode, response);
However i am not sure sending HttpResponseMessage as ObjectResult is correct way.
I also want to make sure content negotiation will work.
Update 7/25/2022
Updated the correct answer
public class HttpResponseMessageResult : IActionResult
{
private readonly HttpResponseMessage _responseMessage;
public HttpResponseMessageResult(HttpResponseMessage responseMessage)
{
_responseMessage = responseMessage; // could add throw if null
}
public async Task ExecuteResultAsync(ActionContext context)
{
var response = context.HttpContext.Response;
if (_responseMessage == null)
{
var message = "Response message cannot be null";
throw new InvalidOperationException(message);
}
using (_responseMessage)
{
response.StatusCode = (int)_responseMessage.StatusCode;
var responseFeature = context.HttpContext.Features.Get<IHttpResponseFeature>();
if (responseFeature != null)
{
responseFeature.ReasonPhrase = _responseMessage.ReasonPhrase;
}
var responseHeaders = _responseMessage.Headers;
// Ignore the Transfer-Encoding header if it is just "chunked".
// We let the host decide about whether the response should be chunked or not.
if (responseHeaders.TransferEncodingChunked == true &&
responseHeaders.TransferEncoding.Count == 1)
{
responseHeaders.TransferEncoding.Clear();
}
foreach (var header in responseHeaders)
{
response.Headers.Append(header.Key, header.Value.ToArray());
}
if (_responseMessage.Content != null)
{
var contentHeaders = _responseMessage.Content.Headers;
// Copy the response content headers only after ensuring they are complete.
// We ask for Content-Length first because HttpContent lazily computes this
// and only afterwards writes the value into the content headers.
var unused = contentHeaders.ContentLength;
foreach (var header in contentHeaders)
{
response.Headers.Append(header.Key, header.Value.ToArray());
}
await _responseMessage.Content.CopyToAsync(response.Body);
}
}
}
You can create a custom IActionResult that will wrap transfere logic.
public async Task<IActionResult> Routes([FromBody]JObject request)
{
var httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("");
// Here we ask the framework to dispose the response object a the end of the user resquest
this.HttpContext.Response.RegisterForDispose(response);
return new HttpResponseMessageResult(response);
}
public class HttpResponseMessageResult : IActionResult
{
private readonly HttpResponseMessage _responseMessage;
public HttpResponseMessageResult(HttpResponseMessage responseMessage)
{
_responseMessage = responseMessage; // could add throw if null
}
public async Task ExecuteResultAsync(ActionContext context)
{
context.HttpContext.Response.StatusCode = (int)_responseMessage.StatusCode;
foreach (var header in _responseMessage.Headers)
{
context.HttpContext.Response.Headers.TryAdd(header.Key, new StringValues(header.Value.ToArray()));
}
if(_responseMessage.Content != null)
{
using (var stream = await _responseMessage.Content.ReadAsStreamAsync())
{
await stream.CopyToAsync(context.HttpContext.Response.Body);
await context.HttpContext.Response.Body.FlushAsync();
}
}
}
}
ASP.NET Core has the return object RedirectResult to redirect the caller.
Simply wrap the response in Ok() Action return type:
return Ok(response)
so your code would look something like:
[Route("api/v1")]
public class RoutesController : Controller
{
[HttpPost]
[Route("routes")]
public async Task<IActionResult> Routes([FromBody]JObject request)
{
var httpClient = new HttpClient();
Task<HttpResponseMessage> response = await httpClient.GetAsync(request["resource"]);
return Ok(response);
}
}
More info here: https://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-3.1

400 Bad Request when trying to send api request with IFormFile in request object

I am having a hard time figuring out how to send an IFormFile object part of the request. It is an API call to upload an image. I have found a few resources and have tried each suggestion but I always get a 400 Bad Request response when I try and hit the API. Both the API and client are ASP.NET Core 2.1
Call to the API
public async Task<ApiResponse<ImageDto>> AddImageToWebsite(AddImageToWebsiteRequest request)
{
try
{
HttpClient client = new HttpClient();
var url = $"{_apiInfo.Url}/portal/AddImageToWebsite";
byte[] data;
using (var br = new BinaryReader(request.Image.OpenReadStream()))
{
data = br.ReadBytes((int) request.Image.OpenReadStream().Length);
}
var bytes = new ByteArrayContent(data);
MultipartFormDataContent multiContent = new MultipartFormDataContent();
multiContent.Add(bytes, "file", request.Image.FileName);
multiContent.Add(new StringContent(request.WebsiteId.ToString()), "WebsiteId");
multiContent.Add(new StringContent(request.AltText), "AltText");
// BREAKS AFTER THIS POST CALL
var apiResponse = await client.PostAsync(url, multiContent);
// DESERIALIZE RESPONSE TO RESPONSE OBJECT HERE
}
catch (Exception ex)
{
Log.Error(ex, "Error calling api");
return ApiResponse.InternalError<ImageDto>(ex.Message);
}
}
AddImageToWebsiteRequest
public class AddImageToWebsiteRequest
{
public int WebsiteId { get; set; }
public IFormFile Image { get; set; }
public string AltText { get; set; }
}
API CALL
[HttpPost]
[Route("AddImageToWebsite")]
public async Task<JsonResult> AddImageToWebsite(AddImageToWebsiteRequest request)
{
return await this.HandleRequest(async () =>
{
var website = _dataAccess.GetWebsite(request.WebsiteId);
if (website == default(Website))
{
return ApiResponse.NotFound<ImageDto>("Website not found");
}
// UPLOAD IMAGE CODE HERE
}
}
It does not even hit the API call. I also tried posting it as follows, and it worked as long as I did not have an image in the serialized object.
Another Attempt
var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
var apiResponse = await client.PostAsync(url, stringContent);
// DESERIALIZE RESPONSE TO RESPONSE OBJECT HERE
I have tried so many different recommendations online and none seem to work.
IFormFile is only for multipart/form-data encoded POST requests, i.e. a traditional form post. If you're sending JSON, your "upload" needs to be a Base64 string and you need to bind to a byte[]:
public class AddImageToWebsiteRequest
{
public int WebsiteId { get; set; }
public byte[] Image { get; set; }
public string AltText { get; set; }
}
JsonConvert.SerializeObject will automatically convert byte[]s into Base64 strings.
How are you sending this from the view? If you are using a form, you can just give it the multipart/form-data type, give the input type of file and then bind it to the IFormFile in the parameter.
View:
<form id="fileupload" action="yourpath/AddImageToWebsite/" method="POST" enctype="multipart/form-data">
<button type="submit" class="btn btn-primary start">
</button>
<input type="file" name="YourFile"/>
<!--Whatever other things you need to input, use hidden fields-->
</form>
Controller:
[HttpPost]
[Route("AddImageToWebsite")]
public async Task<JsonResult> AddImageToWebsite(IFormFile YourFile)
{
//Do what you need....
}