Posts

Showing posts from 2023

Node Js - Valid JSON body before it reaches API router endpoint "SyntaxError: Unexpected string in JSON at position 83
   at JSON.parse"

Image
When using node js with express, sometimes we get some unexpected errors from the API endpoint which doesn't really makes sense to the API consumer. On the other hand, we need to send appropriate message and Http Code to the consumer. If you pass invalid Json body to a node Js API, you may get the following enexpected error. " SyntaxError: Unexpected string in JSON at position 83 < br >  &nbsp; &nbsp;at JSON.parse" We can clearly see, such errors can't be handled accurately by the consumer application as it has lot of HTML data as response. To fix this, you can try to handle the parser error in the router endpoint and return apropriate message to the consumer, but there is an issue, the middleware itself returns the above error to the consumer, before it reaches the API end point. So, the solution is pretty simple and will be applicable to all the APIs. Write a middleware to handle this error like following. const app = express (); app. use (bodyParser.

Solved : 'WebApplication' does not contain a definition for 'UseKestrel' and the best extension method overload 'WebHostBuilder Kestrel Extensions.UseKestrel(IWebHostBuilder, Action)' requires a receiver of type 'IWebHostBuilder'

I had a requirement to upload files to .NET Core controller using Ajax call. Everything works well when the file size is < 30 Mb. When we try to upload larger files, the controller doesn't receive any files in the POST. Its always null. IList < IFormFile > files var file = Request.Form.Files[ 0 ]; All the values were null, I tried using Files as parameter to the post and also tried using " Request.Form ". After some research, I found that the upload limit of the Kestrel server need to be increased as it limits it by default set to 30 or 50 Mb(not very sure). When trying to add Kestrel  to the builder configuration, I was getting following error: "' WebApplication' does not contain a definition for 'UseKestrel' and the best extension method overload 'WebHostBuilder Kestrel Extensions.UseKestrel(IWebHostBuilder, Action<KestrelServerOptions>)' requires a receiver of type 'IWebHostBuilder '" SOLUTION: My .Net SDK vers