Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Login: thiago.pires@gmail.com
- // Password: thiago123
- // Primeiro você deve fazer o login para pegar o token de autenticação:
- // url: "https://carsystemapp.herokuapp.com/users/sign_in"
- // type: "GET"
- // dataType: "JSON"
- // data: {
- // user: {
- // email: email,
- // password: password
- // }
- // }
- // Na requicisão acima, irá retornar apenas o parâmetro authentication_token
- // É necessário que em toda a requisição abaixo, esteja presente no request header os 2 parâmetros "X-USER-EMAIL" e "X-USER-TOKEN", que serve como autenticação segura.
- // Listar todos os carros
- // url: "/cars"
- // type: "GET"
- // dataType: "JSON"
- // data: {}
- // Cadastrar um carro
- // url: "/cars"
- // type: "POST"
- // dataType: "JSON"
- // data:
- // car: { name: "Onix", brand: "Chevrolet", year: 2016 }
- // Ver informações de um carro
- // url: "/cars/<id>"
- // type: "GET"
- // dataType: "JSON"
- // Atualizar informações de um carro
- // url: "/cars/<id>"
- // type: "PUT"
- // dataType: "JSON"
- // data:
- // car: { name: "New Onix", brand: "Chevrolet", year: 2016 }
- // Deletar um carro
- // url: "/cars/<id>"
- // type: "DELETE"
- // dataType: "JSON"
- // data: {}
- // Exemplo em JavaScript, pode ser testado através do web console:
- var token = null;
- var email = "thiago.pires@gmail.com"
- var passw = "thiago123"
- var insert_cars = function(_email, _passw){
- var new_cars = [
- { name: "Civic 2.0", brand: "Honda", year: 2016 },
- { name: "Corola", brand: "Toyota", year: 2016 },
- { name: "Onix", brand: "Chevrolet", year: 2016 }
- ]
- new_cars.forEach(function(i){
- $.ajax({
- url: "https://carsystemapp.herokuapp.com/cars",
- type: "POST",
- dataType: "JSON",
- data: {
- car: i
- },
- beforeSend: function(xhr){
- // Setando email e token no request header para autenticação
- xhr.setRequestHeader('X-USER-EMAIL', _email);
- xhr.setRequestHeader('X-USER-TOKEN', _passw);
- },
- success: function(data) {
- console.log("Data (insert_cars): ", data);
- },
- error: function(err) {
- console.error("Error (insert_cars): ", err);
- }
- });
- });
- };
- var get_data = function(_email, _passw){
- // inserção de carros para teste
- insert_cars(_email, _passw);
- $.ajax({
- url: "https://carsystemapp.herokuapp.com/cars",
- type: "GET",
- dataType: "JSON",
- beforeSend: function(xhr){
- // Setando email e token no request header para autenticação
- xhr.setRequestHeader('X-USER-EMAIL', _email);
- xhr.setRequestHeader('X-USER-TOKEN', _passw);
- },
- success: function(data) {
- console.log("Data (get_data): ", data);
- },
- error: function(err) {
- console.error("Error (get_data): ", err);
- }
- });
- };
- // Login
- $.post({
- url: "https://carsystemapp.herokuapp.com/users/sign_in",
- dataType: "JSON",
- data: {
- user: {
- email: email,
- password: passw
- }
- },
- success: function(dt){
- console.log("Data (log_in): ", dt);
- // Recebendo token e guardando em variável
- token = dt.authentication_token;
- // Chamando função passando como parâmetro email e token de validação
- get_data(email, token);
- },
- error: function(err){
- console.error("Error (log_in): ", err);
- }
- })
Add Comment
Please, Sign In to add comment