簡體   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