HTTP状态码302 [英] HTTP status code 302

查看:255
本文介绍了HTTP状态码302的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ruby中使用Rails后端,我想将数据发布到此服务器.但是,如果我使用PAW进行后期请求,则会被重定向.我是Http请求的新手.有人可以向我解释一下功能以及如何使用http发表请求吗?

Im working on my Rails Backend in Ruby and i want to post Data to this server. But if i do a Post-request with PAW i get redirected. Im a newbie to Http Requests. Could someone explain me the functionality and how to use http post requests?

我想在服务器的datanase(sqlite3)上发布信息.

i want to post information on my server's datanase (sqlite3).

下面是一个屏幕快照,其中应该解释了所有内容:

Here's a screenshot which should explain everything:

这是如何工作的?请解释 :) 谢谢. 约翰问候

how does this work? please explain :) thanks. greetings John

这是代码:

OwnersController:

OwnersController:

#app/controllers/owners_controller.rb
class OwnersController < SessionsController
     respond_to :html
     before_action :owner_find, only: [:show, :edit, :update, :destroy]

     def index
        @owners = Owner.all
     end

     def show
     end  

    def update
       @owner = Owner.find(params[:id])

       if @owner.update(owner_params)
          redirect_to @owner
       else
          render 'edit'
       end
    end

    def new
       @owner = Owner.new
    end

    def destroy 
       @owner.destroy
       redirect_to owners_path
    end

    def edit
    end

    def create
        @owner = Owner.new owner_params
        if @owner.save!
           flash[:notice] = 'You signed up successfully'
           flash[:color]= 'valid'
           redirect_to owners_path
        else
           flash[:notice] = 'Form is invalid'
           flash[:color]= 'invalid'
          render 'new'
        end
    end

  private 

  def owner_find 
     @owner = Owner.find(params[:id])
  end  

  def owner_params
     params.require(:owner).permit(:name, :password, :password_confirmation, :token)
  end
end

SessionController:

SessionController:

class SessionsController < ApplicationController
  before_filter :authenticate_user, :except => [:login, :login_attempt]

  def login
    #goes to Login Form
  end

  def logout
    session[:owner_id] = nil
    redirect_to :action => 'login'
  end

  def login_attempt
    authorized_user = Owner.authenticate_by_name(params[:login_name],params[:login_password])
    if authorized_user
      session[:owner_id] = authorized_user.id
      flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.name}"
      redirect_to welcome_index_path
    else
      flash[:notice] = 'Invalid Username or Password'
      flash[:color]= 'invalid'
      render 'login'
    end
  end
end

控制台日志:

来自网络请求( http://192.168. 2.144:3000/owners?name = hans& password = hans321& password_confirmation = hans321 )

在2015-10-01 12:12:18 +0200为192.168.2.144开始GET"/owners?name = hans& password = [FILTERED]& password_confirmation = [FILTERED]" 无法从192.168.2.144渲染控制台!允许的网络:127.0.0.1,:: 1、127.0.0.0/127.255.255.255 由OwnersController#index处理为HTML 参数:{"name" =>"hans","password" =>"[[FILTERED]","password_confirmation" =>"[FILTERED]"} 所有者负载(0.1毫秒)选择所有者".*从所有者"中,在所有者"中."id" =? LIMIT 1 [[["id",2]] 所有者负载(0.1毫秒)选择所有者".*从所有者" 在布局/应用程序中呈现的owner/index.html.erb(1.8ms) 60ms内完成200 OK(查看:58.9ms | ActiveRecord:0.2ms)

Started GET "/owners?name=hans&password=[FILTERED]&password_confirmation=[FILTERED]" for 192.168.2.144 at 2015-10-01 12:12:18 +0200 Cannot render console from 192.168.2.144! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by OwnersController#index as HTML Parameters: {"name"=>"hans", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"} Owner Load (0.1ms) SELECT "owners".* FROM "owners" WHERE "owners"."id" = ? LIMIT 1 [["id", 2]] Owner Load (0.1ms) SELECT "owners".* FROM "owners" Rendered owners/index.html.erb within layouts/application (1.8ms) Completed 200 OK in 60ms (Views: 58.9ms | ActiveRecord: 0.2ms)

这表明200没问题,但数据库中什么也没发生.

It's telling 200 ok but nothing happens in the DB.

来自Paw-Request(所以我可以使用post.顺便说一句.如何在浏览器请求中使用post?

from Paw-Request (so i can use post. btw. how do i use post in browser request?

开始POST "/owners?name = hans& password = [FILTERED]& password_confirmation = [FILTERED]" 在2015-10-01 12:12:45 +0200时为192.168.2.144无法渲染控制台 从192.168.2.144!允许的网络:127.0.0.1,:: 1, 127.0.0.0/127.255.255.255由OwnersController#create作为HTML参数进行处理:{"name" =>"hans","password" =>"[FILTERED]", "password_confirmation" =>"[已过滤]"}无法验证CSRF令牌 真实性重定向到 http://192.168.2.144:3000/过滤链 由于:authenticate_user呈现或重定向而停止运行已完成302 在1毫秒内找到(ActiveRecord:0.0毫秒)

Started POST "/owners?name=hans&password=[FILTERED]&password_confirmation=[FILTERED]" for 192.168.2.144 at 2015-10-01 12:12:45 +0200 Cannot render console from 192.168.2.144! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by OwnersController#create as HTML Parameters: {"name"=>"hans", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"} Can't verify CSRF token authenticity Redirected to http://192.168.2.144:3000/ Filter chain halted as :authenticate_user rendered or redirected Completed 302 Found in 1ms (ActiveRecord: 0.0ms)

似乎CRSF身份验证失败.

It seems that the CRSF authentication failed..

首先: 致富佩克!这对我有很大帮助.谢谢!!非常感谢您的努力.

at first: to Rich Peck! This helped me so much. Thank you!! I really appreciate your effort.

我靠近解决方案..我的问题是:我无法在URL中放入正确的参数.令牌认证已禁用以进行测试.所以没关系.

Im near to the solution.. My problem is: i cant put the correct params in the url. The token-auth is disabled for testing. so it wont matter.

参数应类似于: 参数:{"utf8" =>✓","authenticity_token" =>"q9JvFhoSUgfydFTvh18JHbIIdKNDjnOS9m/trVBu9EHPP04xGsO69zPh1BFZBI1Ev1YcnOTiPmaAiPWOSkm5Xg =""","password_confirmation" =>"[已过滤]"},提交" =>创建所有者"}

the params should be like: Parameters: {"utf8"=>"✓", "authenticity_token"=>"q9JvFhoSUgfydFTvh18JHbIIdKNDjnOS9m/trVBu9EHPP04xGsO69zPh1BFZBI1Ev1YcnOTiPmaAiPWOSkm5Xg==", "owner"=>{"name"=>"Hubert", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create Owner"}

,而不是我的要求: 参数:{"name" =>"Hubert","password" =>"[FILTERED]","password_confirmation" =>"[FILTERED]","owner" => {}}

and not as in my request: Parameters: {"name"=>"Hubert", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "owner"=>{}}

推荐答案

HTTP状态代码

首先,30x响应表示资源已移动" .

301响应被许多SEO人用来表示资源的永久迁移. 302不太常见,但仍然意味着类似的事情.

301 responses are used by many SEO people to denote permanent relocation of resources. 302 not so common, but still means a similar thing.

每次发送&接收HTTP请求,您将收到状态码.典型的是200响应-状态成功

Every time you send & receive HTTP requests, you're going to receive a status code. The typical is the 200 response -- status success!

您看到的是 redirect_to 运行中的命令-

What you're seeing is the redirect_to command in action -

if @owner.save!
   flash[:notice] = ...
   redirect_to owners_path

我以前从未使用过PAW,但是我想它只是为您提供服务器的纯响应,在这种情况下,这将是30x 资源已移动" 代码.

I've never used PAW before, but I assume it's just giving you the pure response of the server, which would in this case be a 30x "Resource Moved" code.

我希望典型的浏览器请求能够加载重定向的路由并在屏幕上显示其收益.

I would expect a typical browser request to load the redirected route and display its yield on the screen.

服务器

作为一种测试方法,您应该在浏览器中尝试相同的事务:

As a way to test this, you should attempt the same transaction in your browser:

lvh.me:3000/orders

( lvh.me是路由到您的域自己的本地主机(可帮助解决Rails中的子域)

(lvh.me is a domain routed to your own localhost which helps with subdomains in Rails)

这将使您能够测试并查看响应发生了什么.您*应该*发现您的数据已保存到数据库中(尽管您使用的是SQLite3).

This will give you the ability to test and see what happens with the responses. You *should * find that your data has been saved to the database (albeit SQLite3 in your case).

语法

最后,您需要确保在代码中使用正确的语法.

Finally, you need to ensure you're using the correct syntax in your code.

特别是:

#app/controllers/owners_controller.rb
class OwnersController < ApplicationController
   ...
   def create
      @owner = Owner.new owner_params
   end

   private

   def owner_params
      params.require(:owner).permit(:name, :password, :password_confirmation)
   end
end

您还需要查看 bcrypt-ruby ,以保护您的密码.

You'll also want to look at bcrypt-ruby for protecting your passwords.

测试

我倾向于只使用标准的浏览器功能来测试我的Rails应用.

I tend to just test my Rails apps with standard browser functionality.

这意味着您可以运行 Rails Server (您的控制台),然后您就可以通过浏览器进行访问了.

This means you can run the Rails Server ($ rails s in your console), which you'll then be able to then access through your browser.

您正在尝试使用PAW,这没关系,但是在应用程序的用户交互性(例如,提交真实表单等)方面并没有给您太多灵活性...

You're trying to use this PAW thing, which is okay, but doesn't give you much flexibility in regard to the user-interactivity of the app (for example, submitting real forms etc)...

就您而言,我将执行以下操作:

In your case, I'd do the following:

#app/views/orders/new.html.erb
<%= form_for @order do |f| %>
   <%= f.text_field :name %>
   <%= f.password_field :password %>
   <%= f.password_field :password_confirmation %>
   <%= f.submit %>
<% end %>

然后您将访问lvh.me:3000/orders/new并提交表单.这将向您展示其响应方式!

You'd then access lvh.me:3000/orders/new and submit the form. This will show you how it responds!

HTTP

好的,这是处理HTTP请求的方法...

Okay here's the deal with HTTP requests...

每当您向Web应用程序发送一条事务数据时,您都将通过HTTP请求来进行处理. HTTP请求只是通过"Internet"发送数据的一种方式.

Whenever you send a piece of transactional data to your web application, you do it through an HTTP request. HTTP requests are just a way to send data through the "Internet".

对于基于Rails的应用程序,这意味着每次您在应用程序中执行"某项操作时,您都会确实向您的Web服务器发送HTTP请求. Rails解释此请求并发送响应.这是您的问题所在.

With Rails based apps, this means that every time you "do" something in the app, you're really sending an HTTP request to your web server. Rails interprets this request and sends a response. This response is what your question is about.

您正在询问接收302响应-这是Web服务器说您已被重定向的方式.说实话,这是非常基本的东西;您的浏览器可以处理大部分内容.

You're asking about receiving 302 responses - this is the web server's way of saying you've been redirected. It's pretty basic stuff to be honest; your browser handles most of it.

可以在此处找到一个出色的教程:

A great tutorial can be found here:

好的,那么您的错误如下:

Alright then your error is as follows:

无法验证CSRF令牌的真实性

Can't verify CSRF token authenticity

我稍后可以详细说明,但是现在,您可能希望查找以下解决方案:

I can elaborate more on this later, but for now, you might want to look up this solution: WARNING: Can't verify CSRF token authenticity in case of API development

这篇关于HTTP状态码302的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆