繁体   English   中英

使用curl将数据发布到django 1.9表单

[英]Using curl to post data to django 1.9 form

我正在使用django 1.9。

我有一个使用以下字段的表单:

class UploadFileForm(forms.Form):
    component = forms.ChoiceField(choices=[(int(x.id), x.name) for x in Component.objects.all()])
    title = forms.CharField(max_length=200)
    notes = forms.CharField(max_length=2000, widget=forms.Textarea(attrs={'rows': 5}))
    file = forms.FileField()

当我从浏览器访问它时,我可以完美地使用该表单。

但是当我尝试使用curl填充表单时,我不断收到错误“ 此字段是必需的

<ul class="errorlist"><li>This field is required.</li></ul>
<p><label for="id_title">Title:</label> <input id="id_title" maxlength="200" name="title" type="text" /></p>
<ul class="errorlist"><li>This field is required.</li></ul>
<p><label for="id_notes">Notes:</label> <textarea cols="40" id="id_notes" maxlength="2000" name="notes" rows="5">
</textarea></p>
<ul class="errorlist"><li>This field is required.</li></ul>
<p><label for="id_file">File:</label> <input id="id_file" name="file" type="file" /></p>
        <button type="submit"> upload file</button>

我的csrdmiddlewaretoken被正确接受,因为我能够在响应输出中看到它。

以下是我尝试的不同curl请求:

`curl <url> \
-X POST -H "Content-Type: application/json" \
-H "Accept: text/html,application/json" \
-H "X-CSRFToken: <token grabbed from form page source>" \
-H "Cookie: csrftoken=<token grabbed from form page source>" \
 -d 'title=testCurl'`

`curl <url> \
-X POST -H "Content-Type: application/json" \
-H "Accept: text/html,application/json" \
-H "X-CSRFToken: <token grabbed from form page source>" \
-H "Cookie: csrftoken=<token grabbed from form page source>" \
 -F 'title=testCurl'`

`curl <url> \
-X POST -H "Content-Type: application/json" \
-H "Accept: text/html,application/json" \
-H "X-CSRFToken: <token grabbed from form page source>" \
-H "Cookie: csrftoken=<token grabbed from form page source>" \
 -d '{"title":"testCurl"}'`

一旦这个工作,我需要找到一种方法来传递文件字段中的文件。

任何人都可以帮我解决这个问题吗?

-----编辑:根据@ohrstrom的建议:

当我从chrome开发人员工具“复制为curl”时,我看到以下内容。

curl 'http://localhost:8000/releases/binary_upload' -H 'Cookie: JSESSIONID=84666B9EE0BB747F04AC3179FEB78F65; csrftoken=E50JjoNz1qigYUehGdxPjnsscCNaFslu' -H 'Origin: http://localhost:8000' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36' -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryn3n6mrAf19RXCh3A' -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:8000/releases/binary_upload' -H 'Connection: keep-alive' -H 'DNT: 1' --data-binary $'------WebKitFormBoundaryn3n6mrAf19RXCh3A\r\nContent-Disposition: form-data; name="csrfmiddlewaretoken"\r\n\r\nE50JjoNz1qigYUehGdxPjnsscCNaFslu\r\n------WebKitFormBoundaryn3n6mrAf19RXCh3A\r\nContent-Disposition: form-data; name="component"\r\n\r\n13\r\n------WebKitFormBoundaryn3n6mrAf19RXCh3A\r\nContent-Disposition: form-data; name="title"\r\n\r\ntest1\r\n------WebKitFormBoundaryn3n6mrAf19RXCh3A\r\nContent-Disposition: form-data; name="notes"\r\n\r\ntest123\r\n------WebKitFormBoundaryn3n6mrAf19RXCh3A\r\nContent-Disposition: form-data; name="file"; filename="Topology_Components.png"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundaryn3n6mrAf19RXCh3A--\r\n' --compressed

但是当我从终端执行相同的命令时,它会显示“提交的文件为空”

========最终编辑========找到解决方案。 将其添加到下面的答案中。

如果您需要更多实施细节,可以在https://github.com/kiran-vemuri/DevServe找到它

默认情况下,Django中的表单字段是必需的:

https://docs.djangoproject.com/en/1.9/ref/forms/fields/#required

您只是在数据中发送“标题”字段,这是唯一没有出错的字段。

发送您要发送的数据中的所有表单字段,或者使字段必需= False。

class UploadFileForm(forms.Form):
    component = forms.ChoiceField(required=False, choices=[(int(x.id), x.name) for x in Component.objects.all()])
    title = forms.CharField(max_length=200)
    notes = forms.CharField(required=False, max_length=2000, widget=forms.Textarea(attrs={'rows': 5}))
    file = forms.FileField(required=False)

而不是直接向Web表单发送POST请求。 我目前正在实施django-restframework

对于所有类型的HTTP请求的viewsets ,我添加了一些额外的文件处理,现在我可以使用以下REST调用来使用curl发送数据。

`
curl -H "Content-Disposition: attachment;" \
-X POST \
-F "name=test_file" \ 
-F "component_id=14" \
-F "notes=Hello World how are you.." \
-F "file=@<path-to-file>" http://localhost:8000/rest/binaries/
`

我过去只传递数据及其值,所以我只使用了这么多:

curl http://127.0.0.1:8000/api/ \
-H "Accept: application/json" \
-d '{"name":"testcurl","ph":"123456789"}'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM