WebApi returning serialized HttpResponseMessage instead of text/html - asp.net

I can't for the life of me figure this out because if I start a fresh WebApi project this works just fine. But in my existing application, it isn't working the same way. I'm dynamically building HTML and returning. Code is below:
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(some html goes here)
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
The response from that code is (200 OK):
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"text/html"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}
Some of the configuration that might be relevant:
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
config.Formatters.Remove(config.Formatters.XmlFormatter);
The request is configured to accept the standard content types.
I've read some things about creating a custom formatter but I think that isn't necessary seeing that I'm able to return HTML just fine in a blank Web API project. I'm thinking something with the middleware or something else in the pipeline is intercepting the object and serializing.
.NET 4.6
NuGet Packges: Microsoft.AspNet.WebApi.* are set to 5.2.5.
This is an issue I'm experiencing in my local environment.
As always, any help is appreciated. Thanks!

Related

How can I get Kendo grid DataSourceRequest object through Ocelot?

There is an Api Gateway that is configured by ocelot. I want to request to the Api with Kendo object and receive that as a Kendo DataSourceRequest. I just did it in Angular and dot net core Api project and It worked properly. However, in this current project I don't have any idea how can I do that via Ocelot. This is my Api method in this below.
[HttpGet]
public ActionResult User_Read([DataSourceRequest] DataSourceRequest request)
{
var data = _unitOfWork.Repository<User>().GetAll();
return Ok(data.ToList().ToDataSourceResult(request, ModelState));
}
ocelot configuration
{
"DownstreamPathTemplate": "/api/auth/User_Read/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 9001
}
],
"UpstreamPathTemplate": "/User_Read/{everything}",
"UpstreamHttpMethod": [ "get" ]
}
and my client request code is like this:
read: function (e) {
if (thisObject.defaultFilter) {
if (e.data.filter) {
var filters = e.data.filter.filters;
var existFilter = filters.filter(function (obj) {
if (obj.field == thisObject.defaultFilter.field) return true;
}).lenght > 0;
if (!existFilter) e.data.filter.filters.push(thisObject.defaultFilter);
}
else {
e.data.filter = { logic: "and", filters: [thisObject.defaultFilter] }
}
}
const params = `${toDataSourceRequestString(e.data)}`;
// datasource read request
thisObject.http.get(thisObject.readUrl + "?" + params).subscribe((res) => {
e.success(res);
});
}
You will find it easy to how send request for reading data in dot net core and angular in kendo grid here
Finally I got the solution on my own. The problem was in Ocelot configuration. I will explain as an answer.
I should have replaced "/api/auth/User_Read/{everything}" with "/api/auth/User_Read?{url}" or any name instead of "url" like "everything".
The important thing was the "?" for mapping routes in Ocelot.

Creating a Linkedin image ugcPost works when posting from localhost but not from AWS ECS

I am creating image shares on company profiles in my java app following the docs here --->https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin#create-an-image-share
The problem that I'm encountering is that after uploading the file successfully(I get 201), from AWS ECS FARGATE container,but posting is successful from localhost. this is my code below:
String mediaUploadResponse = registerMediaUpload(userId, headers);
JsonNode mediaUpload = objectMapper.readTree(mediaUploadResponse);
String uploadUrl = mediaUpload.findPath("uploadUrl").asText();
HttpClient client = HttpClientBuilder.create().build();
HttpPut request = new HttpPut(uploadUrl);
request.addHeader("Content-Type", APPLICATION_OCTET_STREAM_VALUE);
request.setHeader("X-Restli-Protocol-Version", "2.0.0");
request.setHeader("Authorization", requireNonNull(headers.get(AUTHORIZATION)).get(0));
Path tempFilePath = Files.createTempFile("linkedin", null);
try (InputStream fileStream = new URL(fileUrl).openStream()) {
Files.write(tempFilePath, fileStream.readAllBytes());
File tempFile = tempFilePath.toFile();
request.setEntity(new FileEntity(tempFile, IMAGE_PNG));
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == 201) {
log.info("------------------- media upload result {}", response.getEntity());
return mediaUpload;
} else {
log.error("linkedin media upload request failed {}", request);
throw new BadRequestException(response.toString());
}
} finally {
boolean deleted = tempFilePath.toFile().delete();
log.info("------------------- tempfile deleted: {}", deleted);
}
mediaUpload is the result received from registering the media upload by calling POST https://api.linkedin.com/v2/assets?action=registerUpload
I'm checking the asset status using /v2/assets/{asset-id} and I ultimately get:
{
"recipes": [
{
"recipe": "urn:li:digitalmediaRecipe:feedshare-image",
"status": "CLIENT_ERROR"
}
],
"serviceRelationships": [
{
"relationshipType": "OWNER",
"identifier": "urn:li:userGeneratedContent"
}
],
"mediaTypeFamily": "STILLIMAGE",
"created": 1588963432407,
"id": "C4D22AQGIhdXwlSvDZQ",
"lastModified": 1588963433173,
"status": "ALLOWED"
}
But when running from localhost everything works as expected and post shows up on company feed.
Really struggling to understand what could be the issue.
I actually managed to solve the issue in the meantime. And the issue was caused by the fact that fileUrl was a link to an file in a s3 bucket linked as an origin to a cloudfront deployment to which I had direct access. So I used the AmazonS3 s3client to get the inputstream directly.

API always error 408 - Invalid Data Type Sent

I really don't know what happen with this error (I mean by, is this related to the Flutter or Dart or the package that I used or the API that I used). In a nutshell, it always giving me back error code 408 - Invalid data type sent from my flutter App, while I COMPLETELY could done it in Postman and been tried it with PHP.
I just wanna call a normal POST request to my payment gateway API (I use Midtrans).
Here is my result:
From Postman
From my app in Flutter within VS Code
What am I doing wrong? I am completely has no clue about this.
I will give the detail below for anyone who want to try a simple call to this API so you can reproduce this.
Endpoint
https://api.sandbox.midtrans.com/v2/charge
Body
"payment_type": "bank_transfer",
"bank_transfer": {
"bank": "permata",
"va_number": "1234567890"
},
"transaction_details": {
"order_id": "order-101b-{{2020-11-0222rrd324dzz}}",
"gross_amount": 44000
},
"customer_details": {
"email": "richie.permana#gmail.com",
"first_name": "Richie",
"last_name": "Permana",
"phone": "+6281 1234 1234"
},
"item_details": [
{
"id": "item01",
"price": 21000,
"quantity": 1,
"name": "Schema Programmer"
},
{
"id": "item02",
"price": 23000,
"quantity": 1,
"name": "Ayam Xoxoxo"
}
]
}
This is the header
{
'content-type': 'application/json',
'accept': 'application/json',
'authorization':
'Basic U0ItTWlkLXNlcnZlci1kNnJNbGp5ZDBKSDgyOEhBT28tSWkxM0E=',
'cache-control': 'no-cache'
}
My Dart Function Snippet
Future _makePayment() async {
String url = "https://api.sandbox.midtrans.com/v2/charge";
final Map<String, String> header = {
'content-type': 'application/json',
'accept': 'application/json',
'authorization':
'Basic U0ItTWlkLXNlcnZlci1kNnJNbGp5ZDBKSDgyOEhBT28tSWkxM0E=',
'cache-control': 'no-cache'
};
var json = jsonEncode({
"payment_type": "bank_transfer",
"bank_transfer": {"bank": "permata", "va_number": "1234567890"},
"transaction_details": {
"order_id": "order-101b-{{2020-11-0222rrddzz557}}",
"gross_amount": 44000
},
"customer_details": {
"email": "richie#example.com",
"first_name": "Richie",
"last_name": "Permana",
"phone": "+6281 1234 1234"
},
"item_details": [
{
"id": "item01",
"price": 21000,
"quantity": 1,
"name": "Schema Programmer"
},
{"id": "item02", "price": 23000, "quantity": 1, "name": "Ayam Xoxoxo"}
]
});
Response response = await post(
url,
headers: header,
body: json,
);
var res = jsonDecode(response.body);
print(
"payloadddded =>> ${json.runtimeType}\n");
if (res['status_code'] == 201) {
print("ngeheeeee berhasil MIDTRAANSS =>> ${res['status_code']}");
} else {
print("res full =>> ${response.body}");
print("ape kaden => ${res['status_code']} || terosz => $res");
}
}
Note:
For the body, you may be need put more attention on the order_id because it will return some error if you not change the order_id (yes, think it like any other primary key id).
API Docs: https://api-docs.midtrans.com/
I really appreciate any helps or workaround given.
Thank you very much.
Dart's package:http manipulates the content-type header under the hood. If it does the encoding from string to bytes for you (i.e. if body is a string) then it adds a charset=xxx suffix to the content type - it becomes, for example application/json; charset=utf-8.
Apparently your server is allergic to this. (It shouldn't be, but whatever...). You can prove this using the dart:io http client.
var client = HttpClient();
var request = await client.postUrl(Uri.parse(url));
request.headers.set('content-type', 'application/json; charset=utf-8');
request.headers.add(
'authorization',
'Basic U0ItTWlkLXNlcnZlci1kNnJNbGp5ZDBKSDgyOEhBT28tSWkxM0E=',
);
request.add(utf8.encode(json));
var response2 = await request.close();
var reply = await response2.transform(utf8.decoder).join();
print(reply);
client.close();
This gives the same 408 error but does not if the charset suffix is removed.
There's a simple solution. Don't allow http to do the encoding for you - do it yourself.
var response = await http.post(
url,
headers: headers,
body: utf8.encode(json),
);
(Your json apparently needs work, because this now complains that the json is invalid, but that's easy to compare with your working postman.)

Why assert in #PactVerification?

I don't understand the use of assert in #PactVerification. To me it seams more like a complicated way of saying 1 == 1. For example:
import static org.assertj.core.api.Assertions.assertThat;
public class PactConsumerDrivenContractUnitTest {
#Rule
public PactProviderRuleMk2 mockProvider
= new PactProviderRuleMk2("test_provider", "localhost", 8080, this);
#Pact(consumer = "test_consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
return builder
.given("test GET ")
.uponReceiving("GET REQUEST")
.path("/")
.method("GET")
.willRespondWith()
.body("{\"condition\": true, \"name\": \"tom\"}")
}
#Test
#PactVerification()
public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() {
//when
ResponseEntity<String> response
= new RestTemplate().getForEntity(mockProvider.getUrl(), String.class);
//then
assertThat(response.getBody()).contains("condition", "true", "name", "tom");
}
}
So first in "createPact" we state
body("{\"condition\": true, \"name\": \"tom\"}")
Then in givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody annotated #PactVerification we do this
assertThat(response.getBody()).contains("condition", "true", "name", "tom");
But why? We just said that! As far as I can see the assertion does not show up in the generated Pact file. It seams to fill no purpose?
In addition to that, I thought that the idea of contract testing was to reduce the need for integration test since they can break for example if test data changes. But here we still depend on test data. If there are no "Tom" in the Provider, then the test will fail. I primarily wanted to test if the contract is broken, not if the test data has changed.
The example given is a contrived one. In real life using Pact, you wouldn't do this. Your PactVerification would invoke a collaboration method/class/thing which is responsible for the external call to the service you are mocking.
So your assertions are then on what the collaborating function is doing.
Eg. A User Service might create an object with certain properties, that you know only are populated by that external call.
Testing assertions in your #PactVerification test method is not mandatory, yet still it might be very helpful. E.g. you may make a typo in your JSON body string and you wont be able to catch it in your test and it will break provider's pipeline. Assertions in this case have nothing to do with generated Pact file, they play role of a guard that checks in the end if the contract you have just defined (RequestResponsePact) matches all your expectations (assertions).
Also it is worth mentioning that your consumer contract tests should break only if provider tries to release a change that makes your expectations broken. And this is consumer's responsibility to write good contract tests. In your example you have defined following expectation:
#Pact(consumer = "test_consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
return builder
.given("test GET ")
.uponReceiving("GET REQUEST")
.path("/")
.method("GET")
.willRespondWith()
.body("{\"condition\": true, \"name\": \"tom\"}")
}
This contract will be satisfied as long as condition == true and name == tom. This is over-specification of a response. You could define more flexible response with PactDslJsonBody DSL instead:
#Pact(consumer = "test_consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
final DslPart body = new PactDslJsonBody()
.stringType("name", "tom")
.booleanType("condition", true);
return builder
.given("test GET ")
.uponReceiving("GET REQUEST")
.path("/")
.method("GET")
.willRespondWith()
.body(body)
.toPact();
}
This fragment will generate Pact file like:
{
"provider": {
"name": "providerA"
},
"consumer": {
"name": "test_consumer"
},
"interactions": [
{
"description": "GET REQUEST",
"request": {
"method": "GET",
"path": "/"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json; charset=UTF-8"
},
"body": {
"condition": true,
"name": "tom"
},
"matchingRules": {
"body": {
"$.name": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
},
"$.condition": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
}
}
}
},
"providerStates": [
{
"name": "test GET "
}
]
}
],
"metadata": {
"pact-specification": {
"version": "3.0.0"
},
"pact-jvm": {
"version": "3.5.10"
}
}
}
The main difference is that this Pact file uses matchingRules to test if:
type of condition field is boolean
type of name field is String
For strings you can also use PactDslJsonBody.stringMatcher(name, regex, value) method if needed. It allows you to define regular expression that will be tested using current field value.

ASP.NET MVC Sending Json Object with 500 internal status code

Let's firstly look at the code blocks;
Controller function to send json data
[HttpGet]
public ActionResult LoadCourseForDataTable()
{
using (var dd = new DBContext())
{
var data = dd.Courses.ToList();
return Json(new {data = data}, JsonRequestBehavior.AllowGet);
}
}
This block to get json data form the above code block;
<script>
$(document).ready(function () {
$('#CourseTable').DataTable({
"ajax": {
"url": "/Course/LoadCourseForDataTable",
"type": "GET",
"datatype": "json",
},
"columns": [
{ "data": "CourseID", "autoWidth": true },
{ "data": "Title", "autoWidth": true },
{ "data": "Credits", "autoWidth": true },
{ "data": "DepartmentID", "autoWidth": true },
]
});
});
</script>
All the libs which have to be loaded on the client server totally completed and loaded properly. They are not problem in this issue.
The server respond is 500 internal error
Here is the detailed exception;
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
I think everything is true but I have this problem that's interesting. Can you help me? Thank you . . .
This is most likely because you have Lazy Loading enabled. When you are disposing the DBContext you are closing the connection to the database so it can't load any related entities after you close the using statement.
The thing is that the dispose in that using statement is run before the result from the method is passed on. And when the JsonResult try to serialize your courses they will see that there is a Lazy Loaded property and try to load it. But at this point the connection to the database is closed.
I'm strictly against Lazy Loading in web scenarios. For this reason and many others. So the only advice I will give you is disable lazy loading and if you want to include related entities you should fetch those eagerly.

Resources