Skip to content

modify post request use urf-8 encoding #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.offbytwo.jenkins.model.BaseModel;
import com.offbytwo.jenkins.model.Crumb;
import com.offbytwo.jenkins.model.ExtractHeader;
import java.nio.charset.StandardCharsets;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -45,7 +46,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -298,7 +298,8 @@ public void post_form(String path, Map<String, String> data, boolean crumbFlag)

queryParams.add("json=" + EncodingUtils.formParameter(JSONObject.fromObject(data).toString()));
String value = mapper.writeValueAsString(data);
StringEntity stringEntity = new StringEntity(value, ContentType.APPLICATION_FORM_URLENCODED);
StringEntity stringEntity = new StringEntity(value, ContentType.create(ContentType.APPLICATION_FORM_URLENCODED.getMimeType(),
StandardCharsets.UTF_8));
request = new HttpPost(UrlUtils.toNoApiUri(uri, context, path) + StringUtils.join(queryParams, "&"));
request.setEntity(stringEntity);
} else {
Expand All @@ -325,7 +326,7 @@ public void post_form(String path, Map<String, String> data, boolean crumbFlag)
public HttpResponse post_form_with_result(String path, List<NameValuePair> data, boolean crumbFlag) throws IOException {
HttpPost request;
if (data != null) {
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(data);
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(data,StandardCharsets.UTF_8);
request = new HttpPost(UrlUtils.toNoApiUri(uri, context, path));
request.setEntity(urlEncodedFormEntity);
} else {
Expand Down Expand Up @@ -355,7 +356,7 @@ public String post_xml(String path, String xml_data, boolean crumbFlag) throws I
handleCrumbFlag(crumbFlag, request);

if (xml_data != null) {
request.setEntity(new StringEntity(xml_data, ContentType.create("text/xml", "utf-8")));
request.setEntity(new StringEntity(xml_data, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), StandardCharsets.UTF_8)));
}
HttpResponse response = client.execute(request, localContext);
jenkinsVersion = ResponseUtils.getJenkinsVersion(response);
Expand All @@ -373,7 +374,7 @@ public String post_xml(String path, String xml_data, boolean crumbFlag) throws I
*/
@Override
public String post_text(String path, String textData, boolean crumbFlag) throws IOException {
return post_text(path, textData, ContentType.DEFAULT_TEXT, crumbFlag);
return post_text(path, textData, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), StandardCharsets.UTF_8), crumbFlag);
}

/**
Expand Down Expand Up @@ -445,7 +446,7 @@ public void close() {
}
}


/**
* Add authentication to supplied builder.
* @param builder the builder to configure
Expand All @@ -454,7 +455,7 @@ public void close() {
* @param password the password
* @return the passed in builder
*/
protected static HttpClientBuilder addAuthentication(final HttpClientBuilder builder,
protected static HttpClientBuilder addAuthentication(final HttpClientBuilder builder,
final URI uri, final String username,
String password) {
if (isNotBlank(username)) {
Expand All @@ -469,7 +470,7 @@ protected static HttpClientBuilder addAuthentication(final HttpClientBuilder bui
return builder;
}


/**
* Get the local context.
* @return context
Expand All @@ -478,7 +479,7 @@ protected HttpContext getLocalContext() {
return localContext;
}


/**
* Set the local context.
* @param localContext the context
Expand All @@ -487,10 +488,10 @@ protected void setLocalContext(final HttpContext localContext) {
this.localContext = localContext;
}





private <T extends BaseModel> T objectFromResponse(Class<T> cls, HttpResponse response) throws IOException {
InputStream content = response.getEntity().getContent();
byte[] bytes = IOUtils.toByteArray(content);
Expand Down