在AngularJS和bash中使用curl [英] Using curl with AngularJS and bash

查看:52
本文介绍了在AngularJS和bash中使用curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过bash脚本使用curl填充Web表单到使用AngularJS的网站.找不到有关如何执行此操作的任何文档.甚至可以使用curl将数据发布到使用AngularJS的Web窗体中吗?我什至不确定我要问的是正确的问题,还是有更好的方法?

I’m trying to fill out web forms using curl via a bash script to a website that uses AngularJS. Can’t find any documentation on how to do this. Is it even possible to use curl to POST data to webforms that use AngularJS? I’m not even sure I’m asking the right question or if there’s a better method?

推荐答案

在大多数情况下,AngularJS使用带有JSON有效负载的ajax调用,而不是老式的多部分POST.

In most cases AngularJS uses ajax calls with JSON payload instead of old-school multipart POSTs.

您可以使用浏览器发送测试帖子,并将请求信息另存为cURL".

很可能您将具有准备使用的命令,可以将其添加到bash文件中.

Most likely you will have ready-to-use command to add to your bash file.

但是,此类帖子通常与经过身份验证的人相关联,因此您需要在请求中填写最新的会话Cookie.

But quite often such posts are associated with authenticated person so you will need to fill in up-to-date session cookie into your request.

首先要检查的是您的命令是否适用于已清理的cookie.如果可行,那么您的任务就完成了.只需使用如下代码调用此类API:

First things to check will be whether your command works with cleaned cookies. If it works then your task is done. Just call such API with something like this:

curl -X POST -H "Content-Type:application/json" \
 http://some-server/handle-form \
 -d '{"parameter1":["41","34"],"another_parameter":"val1"}'

但是,如果您的curl请求被服务器拒绝且缺少cookie,则您需要在调用API请求之前设置适当的cookie.

But if your curl request is rejected by server with cookies absent then you need to setup proper cookie before invocation of API request.

此调用将针对服务器对您进行身份验证,并将会话cookie存储在jar文件中:

This call will authenticate you against server and will store session cookie in a jar file:

curl -b ./jar -c ./jar -i -X POST -H "Content-Type:application/json" \
 http://some-server/login \
 -d '{"login":"my-user-name", "password":"my-password"}'

这样的保存会话cookie将被重复用于后续的API调用:

And such save session cookies would be reused for subsequent API calls:

curl -b ./jar -c ./jar -i -X POST -H "Content-Type:application/json" \
  http://some-server/handle-form \
  -d '{"parameter1":["41","34"],"another_parameter":"val1"}'

这篇关于在AngularJS和bash中使用curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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