spulec / uncurl

A library to convert curl requests to python-requests.
Apache License 2.0
607 stars 94 forks source link

simplify conversion of --data-binary #19

Closed martibook closed 5 years ago

martibook commented 6 years ago

With my experience, simply converting --data-binary parameter into a string with unchanged content works well. So forgive me to raise this pull request, please. If there's anything you think I'm not clear about yet, you will be appreciated if you don't mind pointing them out.

spulec commented 5 years ago

Is this to fix a particular bug or just a cleanup?

The existing behavior is nice in order to display an easier-to-read representation, but if it is causing issues then we should discuss.

martibook commented 5 years ago

I think I might have figured out what was the failure cause.

Though requests library does support using a dictionary as the container of data parameters, it will later transform this dictionary into something with string format, and turns the type of every kind of value into the string type. for instance:

if we pass a dictionary of data parameters like this data = { "number": 1, "string": "1" }

requests will transform it into something like this before launching the request "data": { "number": "1", "string": "1" }

now that the type of value of key "number" has changed from integer to string, it causes the data parameter no longer what acceptable on server end.

Therefore my suggestion is that, keep the original content of --data-binary as it is in curl command, for the server end might load a json object from data parameters string.

And what's more, sometimes even an EXTRA SPACE inside data parameter string will cause failure to the request, I found this when I played with this example

import requests

requests.post("http://pandahomeios.ifjing.com/action.ashx/otheraction/9028", data='{"CateID":"508","PageIndex":1,"PageSize":30}', headers={ "Accept": "/", "BodyEncryptType": "0", "CUID": "wx_app", "Content-Type": "text/plain; charset=utf-8", "DivideVersion": "1.0", "Host": "pandahomeios.ifjing.com", "IMEI": "wx_app", "IMSI": "wx_app", "MT": "4", "PID": "20000079", "ProtocolVersion": "1.0", "SessionId": "", "Sign": "7876480679c3cfe9ec0f82da290f0e0e", "SupFirm": "5.0.2", "SupPhone": "Redmi Note 3", "User-Agent": "Mozilla/5.0 (Linux; Android 6.0.1; OPPO R9s Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/67.0.3396.87 Mobile Safari/537.36 hap/1.0/oppo com.nearme.instant.platform/2.1.0beta1 com.felink.quickapp.reader/1.0.3 ({packageName:com.oppo.market,type:other,extra:{}})" }, cookies={}, ).text

Adding just a white space between key and value(CateID and 508) would fail this request.

data='{"CateID":"508","PageIndex":1,"PageSize":30}' data='{"CateID": "508","PageIndex":1,"PageSize":30}'

So forgive me to suggest again :) keep the original content please~~

spulec commented 5 years ago

Ah, good to know. Thanks!