I have a Core 2.2 Web API project as my back-end for an Angular front-end. On one screen I allow the user to select x amount of records for processing from a data grid. Before the records can be processed I need to check the database to see if records exist in another table by passing a list of 3 fields (intelligent key) to my API. I put this list into an object array, do a Json.stringify on that object and send it to my API as a Get request. This works fine as long as I select 1-3 records. As soon as I select 4 or more records I get "Access to XMLHttpRequest at 'request url' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
My Cors policy indicates it should let anything through and I'm also confused by why 1-3 records works fine.
In my startup.cs -> ConfigureServices method I have the Cors policy defined like so:
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
In my Angular service I make this call which serializes by array (apiUrl is my url to call my API: https://localhost/api/controller)
getRecordsByIntelligentKey(intelligentKey: IntelligentKey[]): Observable<Record[]>{
const jsonObject = JSON.stringify(intelligentKey);
const url = `${apiUrl}/${jsonObject}`;
return this.http.get<Record[]>(url).pipe(
tap(_ => console.log('fetching records based on intelligent key')),
catchError(this.handleError('getRecordsByIntelligentKey', []))
);
}
In my controller GET action I deserialize my string. I mean I'd like to pass an object but I think I need to do a POST for that.
[HttpGet("{jsonObject}")]
public ActionResult<List<Commission>> GetByCustomerAndTerritoryManager(string jsonObject)
{
try
{
var intelligentKey = JsonConvert
.DeserializeObject<List<ThreeFields>>(jsonObject);
return _repo.GetRecordsByIntelligentKey(intelligentKey);
}
catch (Exception ex)
{
_logger.Error(ex, "Error retrieving records.");
throw;
}
}
Now my problem is the user could select 1000s of records. When I select over 1000 records I just get ERR_CONNECTION_RESET probably due to the querystring being way too long.
I'm thinking I need an object but everything I've researched seems to advise against doing that with a GET and using the POST request instead. Problem is, it's a restful API and I'm already using the POST request for the processing portion. I guess I could use PUT or DELETE but it just feels wrong. I'm going to wire up the PUT right after I post this question to see if it will work but ultimately I'd like to find the correct solution for this.
UPDATE: The PUT method works fine even with over 1000 records selected so I guess this will be my interim solution for now. I still feel like there's code smell and would love to use a GET but at least this allows me to proceed.
I'm trying to write some tests for api but the problem is the response sent from api is ok only for the first request I sent to api, after that, I get
No content was sent from Yii application error.
The problem is I can't use echo directly in my end function (everything is ok if i use echo) because if the response is too long it will be flushed! and now I'm trying to use
Yii::$app->response;
and try to set my response in
$response->content
but in
Yii2::doRequest //the ob_get_clean()
function returns null after first request and throw the No content exception.
Any idea of how I can fix the problem with codeception?
I'm presently working on a phx / phoenix API written in Elixir. And I have created a frontend for the API using React.js. However, I'm getting the below error message in the JS console of the browser.
I have successfully created a user using Postman, so I'm 99% sure the error isn't with the phx project, but rather somewhere with the React project.
I have both the frontend and backend hosted on github. And a .env file will need to be created in the root of the React project with the below line,
REACT_APP_API_URL=http://localhost:4000/api
and was working my way through the following tutorial.
Any and all help would greatly be appreciated.
The output of localStorage.getItem("token") being
eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJVc2VyOjEiLCJleHAiOjE0ODcyODI4ODcsImlhdCI6MTQ4NDY5MDg4NywiaXNzIjoiUGhvZW5peENoYXQiLCJqdGkiOiIwNzFlYzgwYi0wZmYzLTQyYzgtODA3Mi1kNzViZmVhZTg4NWEiLCJwZW0iOnt9LCJzdWIiOiJVc2VyOjEiLCJ0eXAiOiJhY2Nlc3MifQ.NsuqH50HooK8vjFfHtPH9iXSykZ9oYA0ul4b_C5fQtpu_zFvNNy-skcv9HI2i25X-NlB-9xOr-xzh2abnrpYUw
suggests that for some reason, the app stored the token without passing it through JSON.stringify, and calling JSON.parse on this string throws the Unexpected token e error, as expected.
I did not see any localStorage.setItem without JSON.stringify in the current code, so the token was probably stored like that in a previous version of the app. You should try clearing it manually and logging in again.
When we get the non-JSON response, we get such error..
To avoid such error, mention the responseType: Text in your api endpoint call.
This will work,
return this.http.post(`${environment.apiUrl}/login`, user, {responseType: 'text'});
This will not work(If you mention type),
return this.http.post<string>(`${environment.apiUrl}/login`, user, {responseType: 'text'});
This error message usually means you're getting a non-JSON response. If you look at the raw response in the Network tab of your debugger, you should be able to see what you're getting back from the server.
has anyone seen such exception before?
Cannot convert type
Nancy.ErrorHandling.DefaultStatusCodeHandler.DefaultStatusCodeHandlerResult
to Nancy.Response
I'm using nancy.owin with old asp.net mvc site.
this error happens when I'm configuring OWIN middleware using app.Map(path...) method
if I apply nancy middleware directly to the app (IAppBuilder) everything works as expected
but when I use app.Map(path) I get such exception. BUT: as a result in my postman I get 404 The resource you have requested cannot be found.
I am using jsonplaceholder (http://jsonplaceholder.typicode.com/) locally. I am testing my GET and POST calls via extjs rest proxy.
I am able to read data. But when I create a model object and save it, the HTTp code for option is 204 and for POST is 200
BUT data is not stored in database of jsonplaceholder local server.
Any Idea where actual problem exists?
I have worked with json-server and this is the expected results and for your knowledge, here is a detailed explanation of HTTP Status codes Status Code Definitions.