如何发送填充值的html表单

问题描述 投票:-1回答:1

基本上我有一个html形式作为字符串

<form class="form login_form" action="https://app.box.com/api/oauth2/authorize?response_type=code&amp;redirect_uri=http://localhost:8080/&amp;client_id=6n8aeye40bowhwz&amp;state=V8eTbbtXbsV" method="post" name="login_form">
	

      <ul class="container error_container basic_list basic_list_sm hidden"><li class="data plm warning ram pas man"><div class="media pvs"><div class="img icon"><div class="sprite_signup_login_icon_error"></div></div><div class="bd"></div></div></li></ul>

	<div class="field_set login_fields fw center">
		


 <div class="login_user_inputs"><label for="login" class="field_label user_login_field">Email Address</label><div class="user_login_field text_input_with_sprite text_input_with_sprite_16x16 mbm"><input id="login" class="text_input login_email ram field_element " name="login" type="email" title="Email Address" placeholder="Email Address" value="[email protected]"> <label class="icon" for="login" title="Email Address"></label></div><label for="password" class="field_label user_password_field">Password</label><div class="user_password_field text_input_with_sprite text_input_with_sprite_16x16 mbm"> <input id="password" class="text_input login_password ram field_element" name="password" title="Password" placeholder="Password" type="password" autocapitalize="off" autocomplete="off" autocorrect="off" value="passwordstring"> <label class="icon" for="password" title="Password"></label></div> </div> <div class="login_submit_div"><input class="btn btn-primary mhn login_submit fw pvm ram" title="Authorize" value="Authorize" type="submit" name="login_submit"></div>   <input type="hidden" name="dologin" value="1" />  <input type="hidden" name="client_id" value="99hxwc5z7wz" /><input type="hidden" name="response_type" value="code" /><input type="hidden" name="redirect_uri" value="http://localhost:8080/" /><input type="hidden" name="scope" value="root_readwrite manage_groups manage_enterprise_properties manage_app_users manage_managed_users" /><input type="hidden" name="folder_id" value="" /><input type="hidden" name="file_id" value="" /> <input type="hidden" name="state" value="Licg8fhDiFyobsV" />  <input type="hidden" name="reg_step" value="" /><input type="hidden" name="submit1" value="1" /><input type="hidden" name="folder" value="" /><input type="hidden" name="login_or_register_mode" value="login" /><input type="hidden" name="new_login_or_register_mode" value="" /><input type="hidden" name="__login" value="1"><input type="hidden" name="redirect_url" value="/api/oauth2/authorize?response_type=code&amp;redirect_uri=http://localhost:8080/&amp;client_id=99hxwc5z7p9g3z&amp;state=LichV8eTbbtXbsV"><input type="hidden" name="request_token" value="5d6b31164f15f58bebff206db0aa595eaa90f5f9b857860f8ac3e64c85a5b5f4">     <input type="hidden" id="_pw_sql" name="_pw_sql" value=""/> 

	</div>
    <div class="sso_switch option_sso mts pvs phm hidden">
        
        <a href='#' class="sso_on" role="button" tabindex="0">Use Single Sign On (SSO)</a>
        
        <a href="#" class="sso_off" tabindex="0">Use Box account credentials</a>
    </div>
</form>

现在我想将此表单提交到form标签的action参数中指定的url。与从浏览器提交的内容相同。条件是我不应该使用beego网络服务器。提前致谢

html forms go
1个回答
0
投票

如果我理解正确,您必须使用HTML表单而不仅仅是输入值,并且您希望将该表单中包含的输入发送到其他服务器进行处理,并且您还希望使用Go以编程方式执行此操作而不是点击浏览器内的按钮。

如果我做对了,你要做的就是首先从表单中获取输入值,操作URL和方法。你可以通过使用golang.org/x/net/html来做到这一点。

获得所有必需值后,您必须将输入值编码为有效的application/x-www-form-urlencoded字符串,您可以使用net/url包进行编码。

然后你要做的就是用http.NewRequest创建一个http请求,使用form方法和action url作为前两个参数,urlencoded字符串作为它的最后一个body参数。 (你可以使用strings.NewReaderstring变成io.Reader。)

最后在http.Client.Do上用新创建的请求调用http.DefaultClient作为它的参数。


可能有一些第三方软件包可以为您完成大部分工作,因此如果您想避免这样做,请尝试通过github进行搜索。

© www.soinside.com 2019 - 2024. All rights reserved.