如果我尝试将URL传递给包含括号的curl,则会失败并显示错误:
$ curl 'http://www.google.com/?TEST[]=1'
curl: (3) [globbing] illegal character in range specification at pos 29
Run Code Online (Sandbox Code Playgroud)
但是,如果我逃避两个括号,它似乎工作:
$ curl 'http://www.google.com/?TEST\[\]=1'
Run Code Online (Sandbox Code Playgroud)
有趣的是,我使用反斜杠逃脱只有第一托架它与错误代码20497默默地失败:
$ curl 'http://www.google.com/?TEST\[]=1'
$ echo $!
20497
Run Code Online (Sandbox Code Playgroud)
我的问题是如何解决一般情况?是否存在会自动转义URL的参数,或者在传递给curl之前需要转义的字符的描述?
在进入细节之前,我知道Stackoverflow上有很多对话和相关问题.所有这些都以不同的方式帮助我,所以我认为我把我的发现全部放在一起作为一个单独的有组织的常见问题解答来总结我的发现.
当然你知道这些,但我只是把它们写成快速回顾.如果我遗漏了什么,请随时编辑.
当您愿意将对象发送到Web服务或服务器端应用程序时,将使用发布请求.
是将对象从Web浏览器传递到服务器端应用程序的过程.可以使用jQuery Ajax调用或Curl post请求.
最流行的那些日子是JSON和XML.由于XML标记的性质,序列化的xml对象的大小相对较大,因此XML变得越来越不受欢迎.在本FAQ中,主要关注的是JSON2序列化.
Spring框架及其强大的注释使得以有效的方式公开Web服务成为可能.Spring中有很多不同的库.我们关注的是Spring web MVC.
这些是您可以用来在客户端发布帖子请求的工具.即使您计划使用JQuery ajax调用,我建议您使用Curl进行调试,因为它会在发出请求后为您提供详细的响应.
如果您的Web服务不依赖于Java EE模型,则必须使用@RequestBody.如果您正在使用模型并且您的JSON对象已添加到模型中,则可以通过@ModelAttribute访问该对象.仅在您的请求是GET请求或GET和POST请求组合的情况下,您将需要使用@RequestParam/@ PathVariable.
正如您从名称中看到的那样简单,如果您在服务器端方法处理请求后向客户端发送响应,则只需要@ResponseBody.
RequestMappingHandlerAdapter是Spring框架的新映射处理程序,它自Spring 3.1起取代了AnnotationMethodHandlerAdapter.如果您的现有配置仍在AnnotationMethodHandlerAdapter中,您可能会发现此帖子很有用.我的帖子中提供的配置将让您了解如何设置RequestMappingHandlerAdapter.
您需要设置一个消息转换器.这是您的序列化JSON消息体在服务器端转换为本地Java对象的方式.
基本配置从这里开始.转换器是MarshallingHttpMessageConverter和CastorMarshaller的基本配置示例,我已经用MappingJackson2HttpMessageConverter和MappingJacksonHttpMessageConverter替换它们.
我的项目设置方式,我有两个配置文件:
hadlerAdapter bean必须位于MVC Dispatcher XML文件的后面.
<bean name="handlerAdapter"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<ref bean="jsonConverter"/>
</list>
</property>
<property name="requireSession" value="false"/>
</bean> …
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试如何使用Laravel构建RESTful API,而我目前正在创建新用户.这只是一个测试,当我尝试使用Laravel中的验证来验证请求时,我得到了一些结果; 这是结果:
我一直试图通过这段代码创建一个新的:
public function store()
{
$validation = Validator::make(Request::all(),[
'username' => 'required|unique:users, username',
'password' => 'required',
]);
if($validation->fails()){
} else{
$createUser = User::create([
'username' => Request::get('username'),
'password' => Hash::make(Request::get('password'))
]);
}
}
Run Code Online (Sandbox Code Playgroud)
但后来我不知道如何在验证中返回错误.但是当我尝试使用if时,它会继续向我提供图像中显示的HTML validation->fails()
.有没有办法以JSON格式进行验证?
使用cURL从Windows命令行运行以下命令,尝试将新文档发布到现有CouchDB数据库(命名为test)失败:
curl -H "Content-Type: application/json" -X POST "http://127.0.0.1:5984/test" -d {"valid":"json"}
Run Code Online (Sandbox Code Playgroud)
它返回错误:
{"error":"bad_request","reason":"invalid_json"}
Run Code Online (Sandbox Code Playgroud)
JSON是有效的,所以给出了什么?
我在使用bash shell脚本时遇到问题,尝试使用cURL POST可变JSON数据.我是从Mac上运行的.我可以成功发布静态数据,但我似乎无法弄清楚如何合并变量.
为了这些例子,我介绍了<room>和<token>.
此脚本成功运行:
#!/bin/bash
curl -X POST -H "Content-Type: application/json" --data '{ "color":"red", "message":"Build failed", "message_format":"text" }' https://api.hipchat.com/v2/room/<room>/notification?auth_token=<token>
Run Code Online (Sandbox Code Playgroud)
现在,我想介绍一个格式化的日期.这个脚本成功发布,但"$ now"按字面意思发布:即"Build now $ now"而不是"Build failed 10-28-2014"
#!/bin/bash
now=$(date +"%m-%d-%Y")
curl -X POST -H "Content-Type: application/json" --data '{ "color":"red", "message":"Build failed $now", "message_format":"text" }' https://api.hipchat.com/v2/room/<room>/notification?auth_token=<token>
Run Code Online (Sandbox Code Playgroud)
我尝试使用printf格式化JSON有效负载.日期字符串被正确替换.但是,这失败并出现错误:"请求正文无法解析为有效JSON:无法解码JSON对象:第1行第0列(字符0)" - 所以我似乎误用了$ payload.
#!/bin/bash
now=$(date +"%m-%d-%Y")
payload=$(printf "\'{\"color\":\"red\",\"message\":\"Build failed %s\",\"message_format\":\"text\"}\'" $now)
curl -X POST -H "Content-Type: application/json" --data $payload https://api.hipchat.com/v2/room/<room>/notification?auth_token=<token>
Run Code Online (Sandbox Code Playgroud)
最后,我试图评估整个命令.这会因为挂起而失败,可能是因为我在滥用逃脱.我尝试了许多逃避变种.
#!/bin/bash
now=$(date +"%m-%d-%Y")
payload=$(printf "\'{\"color\":\"red\",\"message\":\"Build failed %s\",\"message_format\":\"text\"}\'" $now)
cmd=$(curl -X POST -H \"Content-Type: …
Run Code Online (Sandbox Code Playgroud) 我在烧瓶中设置了一个非常简单的邮政路线:
from flask import Flask, request
app = Flask(__name__)
@app.route('/post', methods=['POST'])
def post_route():
if request.method == 'POST':
data = request.get_json()
print('Data Received: "{data}"'.format(data=data))
return "Request Processed.\n"
app.run()
Run Code Online (Sandbox Code Playgroud)
这是我尝试从命令行发送的curl请求:
curl localhost:5000/post -d '{"foo": "bar"}'
Run Code Online (Sandbox Code Playgroud)
但仍然打印出"收到的数据:"无"".所以,它无法识别我传递的JSON.
在这种情况下是否有必要指定json格式?
当我在特定网址上运行curl时,网站停止响应并且不会生成错误,尽管我已将错误报告设置为打开.我已经尝试将curl超时设置为低值,然后它会生成错误,所以我知道它不会超时.
我想知道的主要事情是,怎么会发生这种情况,我怎么能找出原因呢?
我正在尝试访问的URL是对Factual api的调用,以及我在这里使用的URL
(http://api.factual.com/v2/tables/bi0eJZ/read?api_key=*apikey*&filters= { "类别": "汽车", "$ LOC":{ "内$":{"$中心":[[41,-74],80467.2]}})
将它放入浏览器时可以正常工作.如果您将纬度和经度更改为基本上任何其他值,则PHP脚本将按预期工作.
error_reporting(E_ALL);
ini_set('display_errors', '2');
$url="http://api.factual.com/v2/tables/bi0eJZ/read?api_key=*apikey*&filters={\"category\":\"Automotive\",\"\$loc\":{\"\$within\":{\"\$center\":[[41,-74],80467.2]}},\"website\":{\"\$blank\":false}}";
Echo "\n\n1";
$ch = curl_init($url);
Echo 2;
curl_setopt($ch, CURLOPT_HEADER, 0);
Echo 3;
curl_setopt($ch, CURLOPT_POST, 1);
Echo 4;
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,30);
Echo 5;
$output = curl_exec($ch) or die("hhtrjrstjsrjt".curl_error($ch));
Echo 6;
curl_close($ch);
Echo "out: ".$output;
Run Code Online (Sandbox Code Playgroud) 我不确定这是否可行,但我正在尝试卷曲帖子,但是以json为参数,如下:
curl -X POST 'https://myserver/action?params={"field1":"something","whatever":10,"description":"body","id":"random","__oh__":{"session":"12345678jhgfdrtyui"}}'
Run Code Online (Sandbox Code Playgroud)
但是,我不断收到一些错误curl: (3) [globbing] nested braces not supported at pos
X.
我该怎么做呢?
我的login
终点看起来像
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
print request.form # debug line, see data printed below
user = User.get(request.form['uuid'])
if user and hash_password(request.form['password']) == user._password:
login_user(user, remember=True) # change remember as preference
return redirect('/home/')
else:
return 'GET on login not supported'
Run Code Online (Sandbox Code Playgroud)
当我使用测试时curl
,GET
调用看起来像
? ~PYTHONPATH ? ? 43± ? curl http://127.0.0.1:5000/login/
GET on login not supported
Run Code Online (Sandbox Code Playgroud)
但是POST
,我无法访问表单数据并获取HTTP 400
? ~PYTHONPATH ? ? 43± ? curl -d "{'uuid': 'admin', 'password': …
Run Code Online (Sandbox Code Playgroud)