Skip to content

Add support for multipart/form-data #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion graphql_flask/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def json_encode(self, request, d):
# noinspection PyBroadException
def parse_body(self, request):
content_type = self.get_content_type(request)

if content_type == 'application/graphql':
return {'query': request.data.decode()}

Expand All @@ -110,6 +109,9 @@ def parse_body(self, request):
elif content_type == 'application/x-www-form-urlencoded':
return request.form

elif content_type == 'multipart/form-data':
return request.form

return {}

def _execute(self, *args, **kwargs):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_graphqlview.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import pytest
import json

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

try:
from urllib import urlencode
except ImportError:
Expand Down Expand Up @@ -420,3 +425,17 @@ def test_passes_request_into_request_context(client):
'request': 'testing'
}
}

def test_post_multipart_data(client):
query = 'mutation TestMutation { writeTest { test } }'
response = client.post(
url_string(),
data= {
'query': query,
'file': (StringIO(), 'text1.txt'),
},
content_type='multipart/form-data'
)

assert response.status_code == 200
assert response_json(response) == {'data': {u'writeTest': {u'test': u'Hello World'}}}