Skip to content
Myr

Adding in file support

Before adoption: this commit came in with the history the project adopted, so no landing records it.

#139

authored by Mike Waldner · committed by Mike Waldner · parent 71195bb · Browse files · 1 file+21 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+21 added−8 removed

  • other:+21 added−8 removed

1 file+21 added−8 removed

modified:requests/utils.py+21 added-8 removed
169 unmodified lines
170171def curl_from_request(request):172 """Creates a curl command from the request."""173174 #TODO - Files174 #TODO - OAuth175 #TODO - Cookies176177 #: -L/--location - if there is a redirect, redo request on the new place178 curl = 'curl -L '179180 #: -u/--user - Specify the user name and password to use for server auth.181 #: Basic Auth only for now182 auth = ''183 if request.auth is not None:184 auth = '-u "%s:%s" ' % (request.auth.username, request.auth.password)185
8 unmodified lines
194 header = ''195 if request.headers:196 header = header.join(['-H "%s:%s" ' % (k, v) for k, v in request.headers.iteritems()])197198 data = ''198 form = ''199 if request.method in ('PUT', 'POST', 'PATCH'):200 #: -d/--data - send specified data in post request.201 if isinstance(request.data, (list, tuple)):202 data = data.join(['-d "%s=%s" ' % (k, v) for (k, v) in request.data])203 elif request._enc_data is not None:204 data = '-d %s ' % (request._enc_data)200 #: request.files is updated with request.data if both exist.201 #: ContentType multipart/form-data is used202 if request.files:203 #: -F/--form - Emulate form data. To force 'content' to a file, prefix file name @.204 for k, v in request.files.iteritems():205 if isinstance(v, file):206 form = form + '-F "%s=@%s" ' % (k, v.name)207 elif v not in (None, ''):208 form = form + '-F "%s=%s" ' % (k, v)209210 #: content-type application/x-www-form-urlencoded is used here211 else:212 #: -d/--data - send specified data in post request.213 if isinstance(request.data, (list, tuple)):214 form = form.join(['-d "%s=%s" ' % (k, v) for k, v in request.data])215 elif request._enc_data not in (None, ''):216 form = "-d '%s' " % (request._enc_data)217218 #: Params handled in _build_url207 return curl + auth + method + header + data + '"' + request._build_url() + '"'219 return curl + auth + method + header + form + '"' + request._build_url() + '"'220