2015-07-20 79 views
1

我試圖使用請求將XML文件發送到django視圖。我認爲,該文件正在做派:如何在django中通過POST接收XML文件

headers = {'Content-Type': 'application/xml'} 
response = requests.post('http://localhost:8000/file/', data=file, headers=headers) 

鑑於我:

class ReceiveFile(TemplateView): 
    @method_decorator(csrf_exempt) 
    def dispatch(self, request, *args, **kwargs): 
     print request.read() 
     return HttpResponse('') 

那麼,如何讀文件在這裏查看並再次將其保存爲XML? request.read()爲我提供了發送文件的路徑。

Best,Blake

+1

嘗試['request.body'(https://docs.djangoproject.com/en/1.8/ref/request-response/#django。 http.HttpRequest.body) – Alasdair

+1

你不應該在'dispatch'中這樣做,而是在'post'中。 –

+0

request.body給我的輸出與request.read()相同,這是發送文件的文件的路徑 –

回答

0

您正在發送文件路徑,而不是文件內容。

您需要改爲發送文件的內容。

假設您的文件包含有效的XML:

headers = {'Content-Type': 'application/xml'} 
open_file = open(file,'r') 
file_contents = open_file.read() 
response = requests.post('http://localhost:8000/file/', data=file_contents, headers=headers) 
相關問題