-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trying to call rest api from getserversideprops gives me 401 unauthorized error #13125
Comments
Hi @didemkkaslan thanks for raising this issue! In For instance, you can refactor your code a bit to something like this: import { get } from "aws-amplify/api/server";
const user = await runWithAmplifyServerContext({
nextServerContext: { request: ctx.req, response: ctx.res },
operation: async (contextSpec) =>
get(contextSpec, {
apiName: APINames.PlatformCoreRestApi,
path: '/people/me',
options: {
headers: {
Authorization: `Bearer ${contextSpec.token.value.toString()}`,
},
},
}),
}); Can you try this and see if it resolves the issue? |
In addition to the above example, the |
Hello @chrisbonifacio thanks for the help but I think get only accepts 1 paramater its not really accepting the contextSpec I've tried below approach also with no luck it returns 401 Unauthorized
What worked for me is this one( tho i really dont think its the easiest way):
|
@didemkkaslan awesome! Yes, Thanks @HuiSF for the clarification! |
Hi @didemkkaslan This documentation states that all APIs that are supported to be used on the server side, are exported from the In your use case you should use the Also, please avoid directly using |
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
REST API
Amplify Version
v6
Amplify Categories
api
Backend
CDK
Environment information
Describe the bug
I'm getting unauthorized error trying to access rest api endpoints using getServerSideProps in a NextJS app(pages router).
Expected behavior
It shouldnt be unauthorized
Reproduction steps
Code Snippet
// Put your code below this line.
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response
The text was updated successfully, but these errors were encountered: