0

I have the code below in Python which is making a POST request for an OAuth2 token. It uses basic authentication. The code works fine, but I would like to "translate it" to curl.

The code:

#Authorization: Basic c29tZV91c2VyOnBhc3M=
#some_user:pass = base64decode('c29tZV91c2VyOnBhc3M=') 

def get_access_token():
burp0_url = "https://myurl:443/api/oauth/token"
burp0_headers = {"Accept": "application/json", "Authorization": "Basic c29tZV91c2VyOnBhc3M=", "Content-Type": "application/x-www-form-urlencoded", "Connection": "close", "Accept-Encoding": "gzip, deflate", "User-Agent": "okhttp/3.0.1"}
burp0_data={"grant_type": "client_credentials"}
return json.loads(requests.post(burp0_url, headers=burp0_headers,
data=burp0_data).text)['access_token']

My guess was be that it would look something like this:

curl -v -XPOST -H 'Authorization: Basic c29tZV91c2VyOnBhc3M=' --header 'Accept: application/json' --header 'Connection: close' --header 'Accept-Encoding: gzip, deflate' --header 'User-Agent: okhttp/3.0.1' --data '{"grant_type": "client_credentials"}' https://myurl:443/api/oauth/token

However I keep getting a HTTP/1.1 400 and the following
* Failed writing body (0 != 10)
* Failed writing data
* stopped the pause stream!
* Closing connection 0

Can you help me?

1 Answer 1

0

Looks like you forgot to copy the "Content-Type": "application/x-www-form-urlencoded" header into your curl command.

This would also suggest the data is not submitted as a JSON string as you're currently doing, but as regular form data. You can probably use -F 'grant_type=client_credentials' for that and drop the --data argument.

6
  • I'm not sure if -F option requires the Content-Type to be "multipart". I changed the request to be like curl -v -XPOST -H 'Authorization: Basic c29tZV91c2VyOnBhc3M=' --header "Content-Type: multipart/form-data" --header 'Accept: application/json' --header 'Connection: close' --header 'Accept-Encoding: gzip, deflate' --header 'User-Agent: okhttp/3.0.1' --form "grant_type=client_credentials" https://myurl:443/api/oauth/token (I am getting a 200 now but still getting the * Failed writing body (0 != 10) * Failed writing data * Closing connection 0 - Thanks, though
    – lbrunolx
    Commented Mar 27, 2019 at 12:18
  • Are you piping the results anywhere? A quick Google search seems to indicate that the error means that curl received 10 bytes but was only able to write 0 bytes to the requested output destination (-o). Since you're not providing a -o argument, I'm wondering if perhaps you're sending the output somewhere that's not expecting it or that you don't have permission to write to. Commented Mar 27, 2019 at 19:03
  • See also this question on AskUbuntu which has a similar error output Commented Mar 27, 2019 at 19:05
  • I also tried with the -o. It creates a file with gibberish. Something like ? ?î- -0??¦E÷_ǦmL¶gB¦>%¶¦Éõð"¦+íÎaf¥4½óÍðÂ?+=h¶w}-¦y=Þ¦$6 - Could it be due to the encoding used by the terminal (I'm using Windows)
    – lbrunolx
    Commented Mar 28, 2019 at 15:42
  • That looks like gzip-compressed data, exactly what your Accept-Encoding header is requesting. You should be able to have curl decompress it automatically by adding --compressed to the command Commented Mar 28, 2019 at 19:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.