rest api post method not working for basic auth in meanjs 0.5.0 - meanjs

I tried to check post method of meanjs (0.5.0) in postman with basic auth but it doesn't seem to be working. I am giving the correct credentials.
It show below error:
{
"message": "User is not authorized"
}
Any idea what can be done?

First you should check the policies file defined for that module in /module/your-module/server/policies/ and see if the roles/permissions are ok.
Then check if the credentials you're using belong to a user that has the correct roles.

I found in ./node_modules/passport-local/lib/strategy.js that _usernameField name is usernameOrEmail instead of username.
So I passed below json object with content header as "application/json" from postman and it got connected successfully:
{
"usernameOrEmail":"test",
"password":"test"
}

Related

How do I handle html response from oauth2 token request?

In my angular 6 application, I’m trying to send a request to get an access token from an oauth2 provider called anilist. I’m doing it like this:
this.http.get(url, options).subscribe(result =>
{
console.log('result: ', result);
});
…where http is imported like this:
import { HttpClient } from ‘#angular/common/http';
This results in an error:
SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse
This seems to be because the request is returning an html document (most likely the login page) and it obviously can’t parse it as json.
But then my question is: how does one send a request for an oauth2 token and handle the response as an html page?
Thanks.
It is a bit strange that you want to get access token via http get.
Maybe that is the problem, are you talking about this site? https://anilist.co/
https://anilist.gitbook.io/anilist-apiv2-docs/overview/oauth/authorization-code-grant
The user navigates to their site via your oAuth2 signin link
They sign in, add grant, then the browser redirects to your callback url
You change your auth code to access token via http post request. https://anilist.gitbook.io/anilist-apiv2-docs/overview/oauth/authorization-code-grant#converting-authorization-codes-to-access-tokens
I think you get an html response because
You try to get access token via http get - and here you get an unsupported method / 404 / etc error
Or you are trying to get your access token from the anilist's login page (anilist.co/api/v2/oauth/authorize) instead of the token endpoint (anilist.co/api/v2/oauth/token)
Update
It is the same case, if you are using implicit grant (because you said you are creating an angular app, which can be an SPA - therefore a public client, but in this case the access token is provided in the response) https://anilist.gitbook.io/anilist-apiv2-docs/overview/oauth/implicit-grant

NetSuite RESTlet NLAuth using Postman

I'm new in NetSuite and I tried to make a REST request using Postman I've write in header the NLAuth authorization like the line above:
Authorization : NLAuth nlauth_account=0000000, nlauth_email=at#at.com, nlauth_signature=mypassworld,nlauth_role=3
And i follow the process of this tutorial: https://community.boomi.com/docs/DOC-2676#jive_content_id_To_call_the_getRecord_RESTlet
But I receive the following response:
{
"error": {
"code": "INVALID_LOGIN_ATTEMPT",
"message": "Invalid login attempt."
}
}
Use Postman's built-in Authorization tools to build the correct OAuth1 header: https://www.getpostman.com/docs/v6/postman/sending_api_requests/authorization
It's best practice to use OAuth so credentials are not exposed. NS uses OAuth 1.0.
Realm = NS Account number
Signature Method can be SHA1 or SHA256
Make sure the "Add params to header" is checked.
Always "Update Request" before sending in order to generate proper Nonce/Timestamp
Add the Content-Type = application/json to the headers
Body must be a valid JSON object.
I've used the following doc and it help me: If the URL above don't work click in this link.
URL https://system.xx.netsuite.com/app/help/helpcenter.nl?fid=section_1530099787.html

VM892:1 Uncaught SyntaxError: Unexpected token e in JSON at position 0

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.eyJhdWQiOiJVc2VyOjEiLC‌​JleHAiOjE0ODcyODI4OD‌​csImlhdCI6MTQ4NDY5MD‌​g4NywiaXNzIjoiUGhvZW‌​5peENoYXQiLCJqdGkiOi‌​IwNzFlYzgwYi0wZmYzLT‌​QyYzgtODA3Mi1kNzViZm‌​VhZTg4NWEiLCJwZW0iOn‌​t9LCJzdWIiOiJVc2VyOj‌​EiLCJ0eXAiOiJhY2Nlc3‌​MifQ.NsuqH50HooK8vjF‌​fHtPH9iXSykZ9oYA0ul4‌​b_C5fQtpu_zFvNNy-skc‌​v9HI2i25X-NlB-9xOr-x‌​zh2abnrpYUw
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.

Dynamic Json result from RestAPI not being returned to Angular controller

I have Json data being pulled from a REST API. On success I have created a simple alert that will display the Json results inside of an $http.get. I found a sample URL that points to Json data online for testing and I get the alert with the results just fine. But when I try to do this with my URL pointing back to the api, I get no results (not even an alert). However, when I take that same URL and put it into the browser, all of my Json data is there. Any ideas or thoughts on what might be causing this issue? Thanks.
JavaScript (with test Json data)
var myApp = angular.module('paladinMonitor', ["highcharts-ng"]);
myApp.controller('SizeCtrl', function ($scope, $http, $timeout) {
$http.get('http://ip.jsontest.com/?callback=showMyIP').success(function (data, status) {
alert(data)
});
I had something similar happen to me. Restful web services must use the Access-Control-Allow-Origin header to specify what origins are allowed to access the service. Without it, you can hit the web service successfully by putting the address directly in your browser but it won't work from your app. If your REST service is written in Java, you can see this question for details on how to add the appropriate headers. Other languages will use a similar mechanism.
My other guess is that the web service requires authorization to access. It works fine from your browser because at one time you provided the proper credentials and
your browser cached them. If your service does require authorization, see the "Setting HTTP Headers" section on this page for information on how to add the appropriate headers.
As Alvin Thompson mentioned, you have to set your access-control-allow origin and should also set your access-control-allow-headers, access-control-allow-credentials on the server side. In my case I had to do this in my WebAPIcontroller. This is because in order for CORS to work (cross-domain) you have to have the service 1 (RESTApi in my case) allow permissions for service 2(client) to receive call it. In order, to allow this I had to add the following NuGet packages
NuGet
- Microsoft.AspNet.Cors NuGet package
- Microsoft.Owin.Cors NuGet package
Once these were installed I went to my config file on the API project and added
API App_Start/WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.EnableCors();
}
Then in my controller that inherits the API controller I referenced the NuGet package I installed and enabled CORS on the client side and this is where you set you origin, headers and methods
YourController : ApiController
namespace YourNamespace.Controllers
{
[EnableCors(origins: "https://localhost:.....", headers: "*", methods: "*")]
public class YourController : ApiController
{
//The rest of your controller functionality
}
}
The rest of the issue I was having was how the Json Web Token variable is being passed into my javascript file. I am still working on this, I will post the answer to this as well when I figure it out.
To read more about the CORS issue, this was the best reference for me: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

How to make REST Request to Baasbox server using POSTMAN extension?

I just installed Baasbox on my laptop and everything are work well. But when I try to make a request to create a new user, or anothe REST request using POSTMAN extension, I always got "Invalid Json" error. Is there any mistakes with my request?
it seems that you want to create a user.
In this case you have to set parameters like in the image below.
In particular you have to set "raw" instead of form-data, and also you do not need to set the Authorization header when signing up.
Further info here: BaasBox Signup API