Hi @abhishek_pyro and @juanshishido ! Happy New year too!
Here is a code snippet I added to build the request inside Twitter4JAds:
public BaseAdsResponse updateMembership(String advertiserAccountID, Collection userIDs, String audienceNames, Date effectiveAt, Date expiresAt) throws TwitterException {
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + "1/tailored_audience_memberships";
List<HttpParameter> params = new ArrayList<>();
List<TailoredAudienceMembership> list = buildMembershipBatch(advertiserAccountID, userIDs, audienceNames, effectiveAt,expiresAt);
final String json = listToJson(list);
params.add(new HttpParameter("media", json));
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
Type type = new TypeToken<BaseAdsResponse<TailoredAudienceMembershipUpdate>>() {}.getType();
return twitterAdsClient.executeHttpRequest(baseUrl, params.toArray(new >HttpParameter[params.size()]), type, HttpVerb.POST, headers);
}
Then the headers Map is merged into the request headers in HttpClientImpl:
public HttpResponse post(String url, HttpParameter[] parameters
, Authorization authorization, HttpResponseListener listener, Map<String,String> headers) throws TwitterException {
Map<String,String> mergedHeaders = null;
if (headers != null) {
mergedHeaders = new HashMap<String, String>(this.requestHeaders);
mergedHeaders.putAll(headers);
} else {
mergedHeaders = this.requestHeaders;
}
return request(new HttpRequest(RequestMethod.POST, url, parameters, authorization, mergedHeaders), listener);
}
Then in HttpClientImpl.handleRequest(HttpRequest) I had to add this:
if (con.getRequestProperty("Content-Type") == null) {
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
}
in order to avoid forced “application/x-www-form-urlencoded” content-type.
But this somehow breaks the authentication.
Do you spot any problems with these amendments?
Thanks