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

460 Views Asked by At

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?

2

There are 2 best solutions below

1
Mike W On

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
0
IWilms On

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.