CURL和浏览器

时间:2015-05-23 16:51:48

标签: session curl dart

当我使用CURL和浏览器执行登录请求时,我的服务器行为有所不同。

执行以下卷曲请求时:

 curl -i -X POST 'http://localhost:8080/login' -d 'username=fred&password=blah' -H 'content-type: application/x-www-form-urlencoded'

我得到了以下形式的预期结果: response from curl request

但是,当我从浏览器表单执行相同(或者我认为)请求时,我在浏览器调试器中看到以下结果

canceled request canceled request details

用于发出请求的表单代码如下所示

        <form action="localhost:8080/login" method="POST">
          <input type="text" name="username" placeholder="Username">
          <input type="text" name="password" placeholder="Password">
          <input type="submit" id="sign_in" value="Sign In">
        </form>

自从我设置Access-Controll-Allow-Origin': *以来,我不认为这是请求来源的问题。

可以找到服务器的整个代码here。客户端代码here

我没想法。什么可能导致这个问题?

编辑:正如@GünterZöchbauer在下面的评论中所建议的,通常需要通过HttpRequest进行此类表单提交。我想用一个现成的解决方案来添加答案,然后直接进入另一个问题。 相关的html:

  <form id="login_form" action="/login" method="POST" enctype="application/x-www-form-urlencoded">
    <input type="text" name="username" placeholder="Username">
    <input type="text" name="password" placeholder="Password">
    <input type="submit" id="sign_in" value="Sign In" on-click="{{submitLogin}}">
  </form>

相关的飞镖码:

  /* Login box */
  login_form = querySelector('#login_form');
  login_form.onSubmit.listen(submitLogin);

/* Login code */
void submitLogin(Event e){
  e.preventDefault();

  FormElement form = e.target as FormElement;
  print('inside of submitLogin');

  Uri loginUri = new Uri(scheme: 'http',
      host: 'localhost',
      port: 8080,
      path: 'login');

  print(loginUri);
  var request = new HttpRequest();
  request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  request.request(
      loginUri.toString(),
      method: form.method,
      sendData: new FormData(form)
  ).then(onDataLoaded);
}

void onDataLoaded(HttpRequest req){
  String response = req.responseText;
  print(response);
  //TODO: do visual confiramtion of login
}

当从原始请求(有效)和上面代码完成的那个中映射CURL时,我注意到Content-Type是不同的。 我试图以我所知道的方式强制执行内容类型,但它一直在改进。

curl "http://localhost:8080/login" -H "Origin: http://localhost:8080" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.0 (Dart) Safari/537.36" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryZ1seZo8pq0PZt3Fg" -H "Accept: */*" -H "Referer: http://localhost:8080/" -H "Connection: keep-alive" --data-binary "------WebKitFormBoundaryZ1seZo8pq0PZt3Fg"^
"Content-Disposition: form-data; name=""username"""^

"fred"^
"------WebKitFormBoundaryZ1seZo8pq0PZt3Fg"^
"Content-Disposition: form-data; name=""password"""^

"blah"^
"------WebKitFormBoundaryZ1seZo8pq0PZt3Fg--"^
"" --compressed

而不是。

curl "http://localhost:8080/login" -H "Origin: http://localhost:8080" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.0 (Dart) Safari/537.36" -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Cache-Control: max-age=0" -H "Referer: http://localhost:8080/" -H "Connection: keep-alive" --data "username=fred&password=blah" --compressed

2 个答案:

答案 0 :(得分:3)

action属性中的URL是相对的,这意味着它被附加到当前主机名。因此,如果您已经在http://上,则将/login添加到其中,或将其更改为localhost:8080,将其更改为绝对网址。

答案 1 :(得分:1)

This answer relates to the second part of the question (see the EDIT). I could not find a ready sample code anywhere in the documentation. That's why I add the working code below as an answer. If you find a more elegant way to get the data from the form please write a comment under this post.

HTML part

  <form id="login_form" action="/login" method="POST" enctype="application/x-www-form-urlencoded">
    <input type="text" name="username" placeholder="Username">
    <input type="text" name="password" placeholder="Password">
    <input type="submit" id="sign_in" value="Sign In">
  </form>

The dart code subscribe to event

login_form = querySelector('#login_form');
login_form.onSubmit.listen(submitLogin);

event handler

/* Login code */
void submitLogin(Event e){
  e.preventDefault();

  FormElement form = e.target as FormElement;
  print('inside of submitLogin');

  Uri loginUri = new Uri(scheme: 'http',
      host: 'localhost',
      port: 8080,
      path: 'login');
  InputElement username = querySelector('[name="username"]');
  InputElement password = querySelector('[name="password"]');
  Map dataFromForm = {'username':username.value.toString(),'password':password.value.toString()};
  print(loginUri);
  HttpRequest.postFormData(loginUri.toString(), dataFromForm)
  .then(onDataLoaded);
}

void onDataLoaded(HttpRequest req){
  String response = req.responseText;
  print(response);
  //TODO: do visual