Do a REST request with a URL encoded body and a mime type "application/x-www-form-urlencoded" ? #56
Replies: 1 comment
-
|
As there is not any answer, the wrote an issue report. The complete login method is the following convertUrlSeachParams(
userAuthentication: UserJwtAutenticiation,
): URLSearchParams {
const urlSearchParams = new URLSearchParams()
urlSearchParams.set('username', userAuthentication.username)
urlSearchParams.set('password', userAuthentication.password)
urlSearchParams.set('grant_type', userAuthentication.grant_type as string)
urlSearchParams.set('scope', userAuthentication.scope)
return urlSearchParams
},
async passwordLogin(
userAuthentication: UserJwtAutenticiation,
): Promise<FetchError | undefined> {
const params = this.convertUrlSeachParams(userAuthentication)
const { data, error, status } = await useAuth('/v1/login', {
method: 'post',
body: params.toString() as unknown as UserJwtAutenticiation,
headers: {
...URL_ENCODED_REQUEST_HEADER,
},
watch: false,
})
if (data.value && status.value !== 'error') {
this.bearer = data.value
this.isAuthenticated = true
if (typeof window !== 'undefined' && window?.localStorage) {
window.localStorage.setItem('access_token', data.value.access_token)
window.localStorage.setItem('token_type', data.value.token_type)
}
} else {
this.deleteSession()
}
return error.value as FetchError | undefined
}, |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I try to create a request. My API is the following:
{ "openapi": "3.1.0", "info": { "title": "FastAPI", "version": "0.1.0" }, "servers": [{ "url": "/auth" }], "paths": { "/login": { "post": { "summary": "Auth:Jwt.Login", "operationId": "auth_jwt_login_login_post", "requestBody": { "content": { "application/x-www-form-urlencoded": { "schema": { "$ref": "#/components/schemas/Body_auth_jwt_login_login_post" } } }, "required": true }, "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/BearerResponse" }, "example": { "access_token": "ey...", "token_type": "bearer" } } } }, "400": { "description": "Bad Request", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorModel" }, "examples": { "LOGIN_BAD_CREDENTIALS": { "summary": "Bad credentials or the user is inactive.", "value": { "detail": "LOGIN_BAD_CREDENTIALS" } }, "LOGIN_USER_NOT_VERIFIED": { "summary": "The user is not verified.", "value": { "detail": "LOGIN_USER_NOT_VERIFIED" } } } } } }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } } } } } } }, "components": { "schemas": { "BearerResponse": { "properties": { "access_token": { "type": "string", "title": "Access Token" }, "token_type": { "type": "string", "title": "Token Type" } }, "type": "object", "required": ["access_token", "token_type"], "title": "BearerResponse" }, "Body_auth_jwt_login_login_post": { "properties": { "grant_type": { "anyOf": [ { "type": "string", "pattern": "password" }, { "type": "null" } ], "title": "Grant Type" }, "username": { "type": "string", "title": "Username" }, "password": { "type": "string", "title": "Password" }, "scope": { "type": "string", "title": "Scope", "default": "" }, "client_id": { "anyOf": [{ "type": "string" }, { "type": "null" }], "title": "Client Id" }, "client_secret": { "anyOf": [{ "type": "string" }, { "type": "null" }], "title": "Client Secret" } }, "type": "object", "required": ["username", "password"], "title": "Body_auth_jwt_login_login_post" }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{ "type": "string" }, { "type": "integer" }] }, "type": "array", "title": "Location" }, "msg": { "type": "string", "title": "Message" }, "type": { "type": "string", "title": "Error Type" } }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError" } } } }The generated API client is the following:
I created a Pinia store to call send the login:
When I call the login method, the body is in JSON and not URL encoded.
The request header is
The request body is :
{"username":"[email protected]","password":"***","scope":"","grant_type":"password"}It should be:
grant_type=password&username=me%mydomain.com&password=***
The response as a 422 HTTP error. The body is:
{ "detail": [ { "type": "missing", "loc": [ "body", "username" ], "msg": "Field required", "input": null }, { "type": "missing", "loc": [ "body", "password" ], "msg": "Field required", "input": null } ] }If y send an URL encoded string instead of an object in the body, the IDE linter detects that the format is not as expected.
How to URL encode propery the data with the NuxtOpenFetch API ?
Beta Was this translation helpful? Give feedback.
All reactions