1

I am trying to get the jsoup equivalent of the following curl command

curl -X POST \
  -d '{ "code": "7c7u......" }' \
  -H 'Content-Type: application/json' \
  -u 'userOfApi:passForUser' \
  https://some.api.org.xyz/v2/oauth2/authorizations?

I am trying the following way

Connection con = Jsoup.connect(url)
        .method(Connection.Method.POST)
        .header("Content-Type", "application/json")
        .header("Authorization", "Basic user:pass") // these are base64 encoded
        .requestBody(data)
        .userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
        .ignoreContentType(true);

But I can't seem to pass in the server user and password as specified by the curl -u option. Any idea how I can achieve that as well with Jsoup ? or if not with Jsoup, then any other option.

Saif Asif
  • 5,516
  • 3
  • 31
  • 48
  • 2
    Possible duplicate of [Jsoup connection with basic access authentication](https://stackoverflow.com/questions/7679916/jsoup-connection-with-basic-access-authentication) – Marged May 24 '17 at 14:00

1 Answers1

1

Try debugging the curl request adding -vv parameter and see what's missing in your Jsoup request.

And again, what's your data variable, a String or an Object?

Because, another problem could be the kind of encoding used in sending the data.

freedev
  • 25,946
  • 8
  • 108
  • 125