make request in axios with credentials and data - axios

I need to make this request but in axios
$.ajax({
type: "POST",
url: url,
data: {'HTTP_CONTENT_LANGUAGE': 'en'},
xhrFields: {
withCredentials: true
},
I tried
params = {
data: {
'HTTP_CONTENT_LANGUAGE': 'en',
},
withCredentials: true
};
axios.post(url, params)
But didn't work what do I do?

Related

How to use refresh_token in #nuxtjs/auth-next?

I'm sorry if there are any mistakes because my native language is not English.
I'm using this module with the configuration below, but when I run auth/rerfresh it's requesting using the access_token.
I get the Authorization header on the server side, and when I decode the JWT, the content is access_token.
I am assuming that with this module, when the access_token expires, it will set the refresh_token in the Authorization header and run api/auth/refresh.
Please let me know if there are any mistakes...
auth: {
redirect: {
login: '/login',
logout: '/login',
callback: '/login',
home: '/'
},
strategies: {
local: {
scheme: 'refresh',
autoLogout: true,
token: {
property: 'access_token',
maxAge: 1800,
global: true,
// type: 'Bearer'
},
refreshToken: {
property: 'refresh_token',
data: 'refresh_token',
maxAge: 60 * 60 * 24 * 30
},
user: {
property: false,
autoFetch: true
},
endpoints: {
login: {
url: '/auth/login',
method: 'post',
propertyName: 'access_token',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"grant_type": "password"
},
},
refresh: { url: '/auth/refresh', method: 'get' },
logout: { url: '/auth/logout', method: 'post', },
user: { url: '/auth/me', method: 'get', propertyName: false }
}
},
[/auth/login response]
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzX3Rva2VuIiwiZXhwIjoxNjc0NTI1OTA3LCJzdGFmZl9pZCI6ImFiY2RlMTIzIn0.68BPtgr93lwHgSfSQxieEJUJtGPe9bafQMpnbdHEqy0",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaF90b2tlbiIsImV4cCI6MTY3NzExNzg0Nywic3RhZmZfaWQiOiJhYmNkZTEyMyJ9.71B1iofZIsoaduUOH7ahuTi2gc2NCp5fpsRrsZaGPMg",
"token_type": "bearer"
}
[Cookies]
Name
Value
Expires/Max-Age
Priority
auth._token_expiration.local
1674525907000
Session
Medium
auth._refresh_token.local
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaF90b2tlbiIsImV4cCI6MTY3NzExNzg0Nywic3RhZmZfaWQiOiJhYmNkZTEyMyJ9.71B1iofZIsoaduUOH7ahuTi2gc2NCp5fpsRrsZaGPMg
Session
Medium
auth._token.local
Bearer%20eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzX3Rva2VuIiwiZXhwIjoxNjc0NTI1OTA3LCJzdGFmZl9pZCI6ImFiY2RlMTIzIn0.68BPtgr93lwHgSfSQxieEJUJtGPe9bafQMpnbdHEqy0
Session
Medium
auth._refresh_token_expiration.local
1677117847000
Session
Medium
auth.strategy
local
Session
Medium
I have tried various auth settings in nuxt.config.js but could not solve the problem.
(For example, autoLogout: false, etc.)
I tried running this.$auth.refreshTokens() but , Authorization header seems to be set to access_token.
As you can see in the image above, the token was received and seems to be stored in the cookie.
refresh: { url: '/auth/refresh', method: 'post' },
Changed as above.
I expected refresh_token to be included in the Authorization header, but it seems to be included in the request body.
Changed to refer to the request body in server-side processing.

Sharepoint REST - Delete item from list

i would like to delete item from list in SharePoint 2016. I use SharePoint REST API, but I cannot successfully delete item. This is error message (http code 400):
A node of type 'EndOfInput' was read from the JSON reader when trying to read the start of an entry.A 'StartObject' node was expected.
Here is my code:
$.ajax({
url: 'https://myshp.com/test/_api/web/lists(guid'e23e21c7-ab29-445e-87b8-2b20b721f79d')/items?$filter=ID eq '5'',
type: 'POST',
contentType: 'application/json;odata=verbose',
headers: {
"ACCEPT": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*",
"X-HTTP-Method-Override": "DELETE"
},
success: function (data) {
console.log(data);
},
error: function (error) {
console.log(error);
}
});
What is incorrect? Thanks
Please use below code: It works fine for me :
$.ajax({
url: "https://myshp.com/test/_api/web/lists(guid'e23e21c7-ab29-445e-87b8-2b20b721f79d')/items(5)",
type: 'POST',
contentType: 'application/json;odata=verbose',
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*",
"X-HTTP-Method": "DELETE"
},
success: function (data) {
console.log(data);
},
error: function (error) {
console.log(error);
}
})

How to load a file hosted in a private GitHub repository on Swagger-UI

I want to host on a private website the documentation of my swagger API using swagger-ui.
The YAML file is hosted on a private GitHub repository. According the this gist, I successfully retrieved the desired file with the curl command line but I'm stuck trying it with swagger-ui:
window.swaggerUi = new SwaggerUi({
url: 'https://api.github.com/repos/me/my_repo/contents/api.yaml',
authorizations: {
'Authorization': 'token 0123456789',
'Accept': 'application/vnd.github.v3.raw'
},
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
onComplete: function(swaggerApi, swaggerUi){
if(typeof initOAuth == "function") {
initOAuth({
clientId: "your-client-id",
clientSecret: "your-client-secret-if-required",
realm: "your-realms",
appName: "your-app-name",
scopeSeparator: ",",
additionalQueryStringParams: {}
});
}
if(window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
},
onFailure: function(data) {
log("Unable to Load SwaggerUI");
},
docExpansion: "none",
jsonEditor: false,
defaultModelRendering: 'schema',
showRequestHeaders: false
});
window.swaggerUi.load();
I get an error:
http.js:296 Uncaught TypeError: auth.apply is not a function
Any suggestions?
Here is the answer: (Note that it doesn't work with YAML file. Only JSON.)
// spec: content,
window.swaggerUi = new SwaggerUi({
url: 'https://api.github.com/repos/me/my_repo/contents/api.json',
authorizations: {
'Accept': new window.SwaggerClient.ApiKeyAuthorization("Accept", "application/vnd.github.v3.raw", "header"),
'Authorization': new window.SwaggerClient.ApiKeyAuthorization("Authorization", "token 0123456789", "header"),
},
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
onComplete: function(swaggerApi, swaggerUi){
if(typeof initOAuth == "function") {
initOAuth({
clientId: "your-client-id",
clientSecret: "your-client-secret-if-required",
realm: "your-realms",
appName: "your-app-name",
scopeSeparator: ",",
additionalQueryStringParams: {}
});
}
if(window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
},
onFailure: function(data) {
log("Unable to Load SwaggerUI : ", data);
},
docExpansion: "none",
jsonEditor: false,
defaultModelRendering: 'schema',
showRequestHeaders: false,
});

Multiple routing urls for single service AngularJS

I have multiple URL paths that I would like to map to a single resource. However I am unsure how to change the URL based on the function called. For example the :dest mapping for query would be /allProducts, however destroy would be something along the lines of /delete/:id
service.factory('ProductsRest', ['$resource', function ($resource) {
return $resource('service/products/:dest', {}, {
query: {method: 'GET', params: {}, isArray: true },
save: {method: 'POST'},
show: { method: 'GET'},
edit: { method: 'GET'},
update: { method: 'PUT'},
destroy: { method: 'DELETE' }
});
}]);
For each action you can override the url argument.
Specially for this is the url: {...} argument.
In your example:
service.factory('ProductsRest', ['$resource', function ($resource) {
return $resource('service/products/', {}, {
query: {method: 'GET', params: {}, isArray: true },
save: {method: 'POST', url: 'service/products/modifyProduct'},
update: { method: 'PUT', url: 'service/products/modifyProduct'}
});
}]);
I just needed to put the url in as the param.
service.factory('ProductsRest', ['$resource', function ($resource) {
return $resource('service/products/:dest', {}, {
query: {method: 'GET', params: {dest:"allProducts"}, isArray: true },
save: {method: 'POST', params: {dest:"modifyProduct"}},
update: { method: 'POST', params: {dest:"modifyProduct"}},
});
}]);

ASP.NET MVC 2: jQuery post string is null?

I'm not sure what's the issue, but I'm constructing a string and trying to pass it to my Controller Action. But when the action is executed, the data is null.
JavaScript:
var xml = "<Request><ZipCode>92612</ZipCode></Request>";
$.ajax({
url: "/Home/GetXml",
contentType: 'application/text; charset=utf-8',
data: xml,
success: function (result) { success(result); },
type: "POST",
datatype: "text"
});
Controller:
[HttpPost]
public ActionResult GetXml(string data)
{
if (!String.IsNullOrEmpty(data))
{
return View("Index", data);
}
return View("Index");
}
If I set a breakpoint on the if, the "data" is null. What gives?
The answer: don't use contentType
Thanks to this question and answer:
Asp.Net Mvc JQuery ajax input parameters are null
try with
$.ajax({
url: "/Home/GetXml",
contentType: 'application/text; charset=utf-8',
data: { data: xml },
success: function (result) { success(result); },
type: "POST",
datatype: "text"
});