我是 django Rest Framework (DRF) 的新手,我需要POST使用基于函数的视图 (FDV) 的一些数据。我成功地使用了GET这种方式的方法,但不知道如何使用 POST 方法将值添加到数据库。
# app/models.py
class Item(models.Model):
name = models.CharField(max_length=50)
quantity = models.IntegerField()
price = models.FloatField()
# app/urls.py
urlpatterns = [
path('', views.get_data_list, name='list'),
path('post_val/', views.post_data, name='post_val'), # need to implement
]
# app/serializers.py
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
fields = ('id', 'name', 'quantity','price')
# app/views.py
from django.http.response import JsonResponse
from rest_framework.parsers import JSONParser
from .models import Item
from .serializers import ItemSerializer
from rest_framework.decorators import api_view
@api_view(['GET',]) …Run Code Online (Sandbox Code Playgroud) 我在谷歌上搜索了 React 的 Post 请求,但我得到的只是 React 发送 Post 请求的示例。我需要能够读取发送到反应应用程序的发布请求。我有一个 Get 请求发送到 React 的示例,其中参数位于 this.props.location.search 中。我需要同样的东西,但对于邮政。
与此处描述的Expect Continue问题C#非常类似,HTTP POST返回错误:417"期望失败".,我正在尝试使用Android中的HttpPost对象POST到服务器时出现"期望失败"错误.
在Razor我知道如果你写
@Html.HiddenFor(x => x.PropertyX.PropertyY)
Run Code Online (Sandbox Code Playgroud)
它将生成HTML,如:
<input type="hidden" name="PropertyX.PropertyY" value="...">
Run Code Online (Sandbox Code Playgroud)
并且(特别是)如果这是在编辑器模板中,它可能会生成此HTML:
<input type="hidden" name="ParentProperty[12].PropertyX.PropertyY" value="...">
Run Code Online (Sandbox Code Playgroud)
如何获取任意属性的名称?我假设必须有一些方法来使用MVC基础设施(可能是一些方法或类?)
所以我有以下代码:
var formData = new FormData();
formData.append("title", document.getElementById("title").value);
formData.append("html",my_html);
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://www.mywebsite.com/index");
xhr.send(formData);
xhr.onreadystatechange = function() {
// If the request completed, close the extension popup
if (req.readyState == 4)
if (req.status == 200) window.close();
};
Run Code Online (Sandbox Code Playgroud)
服务器应该以JSON格式发回响应.如何在变量中检索和存储它?
我正在使用URLConnection对象将数据从我的Android客户端发送到服务器.
URL url = new URL("http://10.0.2.2:8080/hello");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());
String s="check"+","+susername;
out.writeObject(s);
out.flush();
out.close();
Run Code Online (Sandbox Code Playgroud)
但我看到很多android程序使用httppost以下列方式发送数据.
HttpClient client=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(LOGIN_ADDRESS);
List pairs=new ArrayList();
String strUsername=username.getText().toString();
String strPassword=password.getText().toString();
pairs.add(new BasicNameValuePair("username", strUsername));
pairs.add(new BasicNameValuePair("password", strPassword));
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response= client.execute(httpPost);
Run Code Online (Sandbox Code Playgroud)
请解释两者之间的差异.如何在servlet中接收后一种情况下的数据.请对这个HttpPost做一个简短的解释.在互联网上,我找到的只是代码.请逐步解释HttpPost及其方法,以及如何在servlet中接收数据.链接会很好.
我想做格式的HTTP POST,如下所示,
<?xml version="1.0" encoding="UTF-8" ?>
<authRequest>
<username>someusernamehere</username>
<password>somepasswordhere</password>
</authRequest>
Run Code Online (Sandbox Code Playgroud)
对于任何基于登录的POST,我通常使用以下机制,
HttpParams params = new BasicHttpParams();
params.setParameter(
"http.useragent",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6");
DefaultHttpClient httpclient = new DefaultHttpClient(params);
HttpPost httppost = new HttpPost("http://mysite.com/login");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("username", "stackoverflow"));
formparams.add(new BasicNameValuePair("password", "12345"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(entity);
HttpResponse httpresponse = httpclient.execute(httppost);
Run Code Online (Sandbox Code Playgroud)
但是通过这种方式,POST数据看起来像,
username=stackoverflow&password=12345
Run Code Online (Sandbox Code Playgroud)
如何根据我上面提到的指定XML格式格式化此请求?
提前致谢.
我正在研究这个需要序列化JSON对象以使用RestSharp发布参数的项目,下面是我的代码:
var request = new RestRequest();
request.Method = Method.POST;
request.RequestFormat = DataFormat.Json;
request.AddBody(jsonObject);
return client.Execute<dynamic>(request);
Run Code Online (Sandbox Code Playgroud)
我意识到的是,不是将每个JSON名称值对添加为post参数,而是request.AddBody将整个JSON字符串添加为一个大的post参数.我的问题是,有没有办法让request.AddBody方法将每个JSON名称 - 值对添加为单独的帖子参数?我知道这request.AddParameter()可以完成工作但需要手动添加每个参数.
代替:
[0]:{
application/json="
{
"name":"john doe",
"age": "12",
"gender": "male"}
}
}
Run Code Online (Sandbox Code Playgroud)
期望的结果:
[0]:"name":"john doe"
[1]:"age":"12"
[2]:"gender":"male"
Run Code Online (Sandbox Code Playgroud) 我想知道是否
' Could not render this component, see the console'
Run Code Online (Sandbox Code Playgroud)
(如第一个图像所示)是错误消息,或者只是可以忽略(或者可能包含很多警告,因此并不总是适用的警告?)。我正在测试上传文件(如第一张图片所示)。正在命中的代码(对应于此文章的nodejs javascript)显示在图1的代码示例中。这非常简单,但是一旦执行了第23行,就会出现“无法渲染”消息。但是,“ Hello World!”的回应 确实会大摇大摆地显示出来。我在网上四处寻找该消息的含义,但是关于此消息的唯一发帖似乎不是在询问该消息,而是在招摇的“服务器响应”部分中的一条错误消息。
//POST
function uploadAzureFile(req, res, next) {
res.json('Hello World');
// res.status(200).send('OK');
}
Run Code Online (Sandbox Code Playgroud)
========回应海伦========
======== CURL ========
在此处找到有关cURL的信息。我如何从curl访问swagger.yaml?
C:\swagger\curl>curl -I "http://localhost:1337/swagger.yaml"
HTTP/1.1 404 Not Found
X-Powered-By: Express
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 26
Date: Sun, 06 Aug 2017 19:33:37 GMT
Connection: keep-alive
C:\swagger\curl>curl -I "http://localhost:1337/"
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 30
ETag: W/"4z-8nd23698"
Date: Sun, 06 Aug 2017 …Run Code Online (Sandbox Code Playgroud)