IWilms
IWilms

Reputation: 600

Problem with HttpURLConnection and application/x-www-form-urlencoded request

I want to send a post request in Groovy using HttpURLConnection and I can't get it to work.

HttpURLConnection conn = new URL("https://url").openConnection()
conn.setDoOutput(true)
conn.setRequestMethod("POST")
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
conn.getOutputStream().write("client_id=client_id&username=username&password=password&scope=api&grant_type=password".getBytes("UTF-8"))
assert conn.getResponseCode() == 403

I have tried other ways: Postman, Java OkHttp, Python requests and Java Unirest and they all worked instantly. Here is the working code for Java Unirest (credits to Postman):

import kong.unirest.HttpResponse

HttpResponse<String> response = Unirest.post("https://url")
        .header("Content-Type", "application/x-www-form-urlencoded")
        .field("client_id", "client_id")
        .field("username", "username")
        .field("password", "password")
        .field("scope", "api")
        .field("grant_type", "password")
        .asString();

I know this is not a particular Groovy question, but maybe somebody knows why my HttpURLConnection version doesn't work?

Upvotes: 0

Views: 901

Answers (2)

IWilms
IWilms

Reputation: 600

Adding conn.setRequestProperty("Accept", "*/*") or conn.setRequestProperty("Accept", "application/json") solved the problem for me. It's a little weird because it works with Postman, where I specifically didn't put that in the header, but who knows what's going on behind the scenes.

I also believe this is specific to the server I'm connecting to. It seems that it doesn't allow an empty Accept header and returns code 403 in that case, which isn't really helpful.

Upvotes: 1

Mike W
Mike W

Reputation: 3932

How about

import java.nio.charset.StandardCharsets

HttpURLConnection conn = new URL('https://url' ).openConnection()
conn.setDoOutput( true )
conn.setRequestMethod( "POST" )
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded")
new DataOutputStream(conn.getOutputStream()).withCloseable {
    it.write( "client_id=client_id&username=username&password=password&scope=api&grant_type=password".getBytes( StandardCharsets.UTF_8 ) )
}

assert conn.getResponseCode() == 200

Upvotes: 1

Related Questions