所以我们有[HttpPost],它是一个可选属性.据我所知,这会限制调用,因此只能通过HTTP POST请求进行调用.我的问题是为什么我要这样做?
我正在尝试在java中为我正在处理的Android应用发布一些JSON数据.以下是有效的还是我需要以不同的方式推送JSON字符串?
HttpPost httpost = new HttpPost("http://test.localhost");
httpost.setEntity(new StringEntity("{\"filters\":true}"));
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
//... other java code to execute the apache httpclient
Run Code Online (Sandbox Code Playgroud)
先感谢您
这是html中的表单示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CSS3 Contact Form</title>
</head>
<body>
<div id="contact">
<h1>Send an email</h1>
<form action="/myaction" method="post">
<fieldset>
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your full name" />
<label for="email">Email:</label>
<input type="email" id="email" placeholder="Enter your email address" />
<label for="message">Message:</label>
<textarea id="message" placeholder="What's on your mind?"></textarea>
<input type="submit" value="Send message" />
</fieldset>
</form>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是在服务器上运行的node.js函数:
var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
switch (req.url)
case '/myaction':
res.end(?????);
break;
} …Run Code Online (Sandbox Code Playgroud) 在您将此作为重复发布之前; 我已经尝试了很多关于SO的建议.
到目前为止,我一直在使用邮递员将数据发布到Java Web服务.这很好用如下:
我现在想用curl做同样的事情,所以我尝试使用以下方法:
$ curl -X POST --data "this is raw data" http://78.41.xx.xx:7778/
$ curl -X POST --data-binary "this is raw data" http://78.41.xx.xx:7778/
$ curl -X POST --data "@/home/kramer65/afile.txt" http://78.41.xx.xx:7778/
$ curl -X POST --data-binary "@/home/kramer65/afile.txt" http://78.41.xx.xx:7778/
Run Code Online (Sandbox Code Playgroud)
不幸的是,所有这些都显示接收方空的原始身体.
有谁知道我在这里做错了什么?我的卷发请求与我的邮递员请求有什么不同?欢迎所有提示!
当我使用以下Python代码向我的Django网站发送POST请求时,我得到403:Forbidden错误.
url = 'http://www.sub.domain.com/'
values = { 'var': 'test' }
try:
data = urllib.urlencode(values, doseq=True)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
except:
the_page = sys.exc_info()
raise
Run Code Online (Sandbox Code Playgroud)
当我打开任何其他网站时,它正常工作.domain.com也是Django网站,也可以正常使用.我认为,这是Django配置问题,任何人都可以告诉我应该怎么做才能提供对我脚本的访问权限?
我们正在就将数据发布到REST端点的问题进行一些讨论.由于对象非常复杂,最简单的解决方案是将它们序列化为JSON并在请求体中发送.
现在的问题是:这是犹太人吗?或者是否应将JSON设置为表格参数,如data = [JSON]?或者在请求体中发送JSON只是为了强迫客户端使用应用程序,通过JavaScript发送数据而不是让浏览器打包它application/x-www-form-urlencoded?
我知道这三个选项都有效.但是,这是OK?或至少推荐?
我试图发布两个参数,使用curl,path和fileName:
curl --request POST 'http://localhost/Service' --data "path='/xyz/pqr/test/'&fileName='1.doc'"
Run Code Online (Sandbox Code Playgroud)
我知道这有什么不对劲.我必须使用像URLEncode这样的东西.我尝试了许多东西仍然没有运气.
请举例说明如何在curl请求的数据中发布url.
我想从java代码登录应用程序.这是我的代码......
String httpsURL = "https://www.abcd.com/auth/login/";
String query = "email="+URLEncoder.encode("abc@xyz.com","UTF-8");
query += "&";
query += "password="+URLEncoder.encode("abcd","UTF-8") ;
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www- form-urlencoded");
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
output.close();
DataInputStream input = new DataInputStream( con.getInputStream() );
for( int c = input.read(); c != -1; c = input.read() )
System.out.print( (char)c );
input.close();
System.out.println("Resp Code:"+con .getResponseCode());
System.out.println("Resp Message:"+ con .getResponseMessage());
Run Code Online (Sandbox Code Playgroud)
但我无法登录,它只返回登录页面.
如果有人可以,请帮助我理解我做错了什么.
调用没有参数的curl,我得到页面输出,即使http状态代码= 404:
$ curl http://www.google.com/linux;
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 404 (Not Found)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>404.</b> <ins>That’s an error.</ins>
<p>The requested URL <code>/linux</code> was not found on this server. <ins>That’s all we know.</ins>
$ echo $?; …Run Code Online (Sandbox Code Playgroud) 我有一个ASP.NET Web API端点,其控制器操作定义如下:
[HttpPost]
public HttpResponseMessage Post([FromBody] object text)
Run Code Online (Sandbox Code Playgroud)
如果我的帖子请求正文包含纯文本(即不应该被解释为json,xml或任何其他特殊格式),那么我认为我可以在我的请求中包含以下标题:
Content-Type: text/plain
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误:
No MediaTypeFormatter is available to read an object of type 'Object' from content with media type 'text/plain'.
Run Code Online (Sandbox Code Playgroud)
如果我将控制器操作方法签名更改为:
[HttpPost]
public HttpResponseMessage Post([FromBody] string text)
Run Code Online (Sandbox Code Playgroud)
我得到一个略有不同的错误消息:
No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'text/plain'.
Run Code Online (Sandbox Code Playgroud) http-post ×10
curl ×3
java ×2
json ×2
linux ×2
post ×2
android ×1
asp.net-mvc ×1
attributes ×1
bash ×1
content-type ×1
django ×1
forms ×1
html5 ×1
http ×1
http-get ×1
http-request ×1
httpclient ×1
node.js ×1
postfile ×1
python ×1
request ×1
rest ×1
text ×1
web-services ×1
wget ×1