简体   繁体   中英

Apache HttpPost isn't sending the query parameters?

I'm using Apache HttpComponents and for some reason when I'm trying to execute an HttpPost with query params, the uri isn't including the parameters.

Here's a code excerpt:

List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>();

parametersBody.add(new BasicNameValuePair("response_type", "code"));

// continue adding parameters...

HttpPost post = new HttpPost("https://www.fitbit.com/oauth2/authorize");
post.setEntity(new UrlEncodedFormEntity(parametersBody, StandardCharsets.UTF_8));

HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(post);

When I check the post's params with EntityUtils.toString(post.getEntity()), all of the parameters are there as expected... however when I execute the post it's navigating to " https://www.fitbit.com/oauth2/authorize?null "

What do I need to do here? If it makes a difference, this is not for an Android app. Thanks for any help.

Edit: For the time being, I'm doing a workaround like so:

String uri = "https://www.fitbit.com/oauth2/authorize?"
             + EntityUtils.toString(new UrlEncodedFormEntity(parametersBody, StandardCharsets.UTF_8);
HttpPost post = new HttpPost(uri);
client.execute(post);

Ask their support. My guess is that their server is broken and cannot understand POST requests with "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" that your UrlEncodedFormEntity is producing.

Their API examples show requests with simple "Content-Type: application/x-www-form-urlencoded" (as produced by many browsers).

Try to set content-type explicitly, there is a setter method

post.getEntity().setContentType("application/x-www-form-urlencoded")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM