I am using OkHTTP to perform a call to a magento API that unfortunately seems not to be supporting url encoding.
Server cannot understand Content-Type HTTP header media type application/x-www-form-urlencoded
My call is as below, how would I prevent URL encoding the RequestBody
RequestBody formBody = new FormBody.Builder()
.add("param", "some_param")
.build();
Request request = new Request.Builder()
.addHeader("Authorization", "Bearer " + token)
.url(url)
.post(formBody)
.build();
Related
I was trying to implement postman's working as following for android app:
Here is my java codes:
MediaType CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded");
RequestBody requestBody = new MultipartBody.Builder()
.setType(CONTENT_TYPE)
.addFormDataPart("phoneNumber", phone)
.addFormDataPart("serviceType", type)
.addFormDataPart("stripeToken", token)
.addFormDataPart("serviceCost", String.valueOf(amount))
.build();
final Request request = new Request.Builder()
.url(Const.URL_HEROKU_BASE+"payment/charge")
.post(requestBody)
.build();
It makes crash ...
I had the same issue when using the public api/login of this free rest service.
In postman, when passing body as application/x-www-form-urlencoded works. But in OkHttp3 MultipartBody doesn't work.
As #shindi suggested this code works perfectly for this api.
RequestBody requestBody = new FormBody.Builder()
.add("email", "some_email")
.add("password", "some_password")
.build();
.header() wasn't needed for the request either. Works for me. Hope it helps.
Change:
MultipartBody
to:
FormBody
I think you are not adding the content type properly:
Change this:
final Request request = new Request.Builder()
.url(Const.URL_HEROKU_BASE+"payment/charge")
.post(requestBody)
.build();
to:
final Request request = new Request.Builder()
.header("Content-Type", "application/x-www-form-urlencoded")
.url(Const.URL_HEROKU_BASE+"payment/charge")
.post(requestBody)
.build();
and remove the .setType(CONTENT_TYPE) part.
Let use FormBody as suggested at https://stackoverflow.com/a/53261129/2013887
And don't forget to use correct #RequestBody type, e.g MultiValueMap for application/x-www-form-urlencoded media type
I'm trying to get response from a node.js rest server (reverse proxy with nginx).
When i try the API through postman, I'm able to read the header parameters in server. But when testing it in Android device, I get the following error:
header undefined
here is the custom header key name
my_basic_key
OKHTTP
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType,
"{\"socialId\":\"1080955243973\",\"provider\":\"google\",\"firstName\":\"iha\",\"lastName\":\"San\",\"email\":\"susi#gmail.com\"}");
Request request = new Request.Builder()
.url("http://<domain>/user/social")
.post(body)
.addHeader("key", "value")
.addHeader("content-type", "application/json")
.addHeader("cache-control", "no-cache")
.build();
NGINX settings
under_score_in_header is ON
I have a asp.net web api url which accepts query string parameters but its actually a post request. I am trying to access the same url in android but not sure how to pass the query parameters. Any help will be appreciated as I am not an android developer basically.
Here is the snippet:
RequestBody formBody = new FormBody.Builder()
.add("email", mEmail.toLowerCase())
.add("password", mPassword)
.build();
//2. Bind the request Object
Request req = new Request.Builder()
.url(loginAPI).post(formBody)
.build();
Response response = client.newCall(req).execute();
This is the solution:
String loginAPI = "http://api.myapi.com/api/authentication?email="+mEmail.toLowerCase()+"&password="+mPassword;
RequestBody reqbody = RequestBody.create(null, new byte[0]);
Request req = new Request.Builder()
.url(loginAPI)
.method("POST", reqbody)
.build();
Response response = client.newCall(req).execute();
I have url like this.
http://host/parallel/team/:team_number.json
and it has post params.
like team_number, team_name.
How to make a post request such that i replace team_number to team number with a value.
Does :team_number need to be handled differently ?
So far i have done
RequestBody formBody = new FormBody.Builder()
.addEncoded(TEAM_NUMBER,tracking_number)
.add(TRACK_NAME, name)
.build();
Request request = new Request.Builder()
.url(SEND_TRACKING_DATA)
.post(formBody)
.build();
Response response = CoreApplication.okHttpClient.newCall(request).execute();
return response.body().string();
How is it possible to append params to an OkHttp Request.builder?
//request
Request.Builder requestBuilder = new Request.Builder()
.url(url);
I've managed the add header but not params.
Here is a complete example on how to use okhttp to make post request (okhttp3).
To send data as form body
RequestBody formBody = new FormBody.Builder()
.add("param_a", "value_a")
.addEncoded("param_b", "value_b")
.build();
To send data as multipart body
RequestBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("fieldName", fileToUpload.getName(),RequestBody.create(MediaType.parse("application/octet-stream"), fileToUpload))
.build();
To send data as json body
RequestBody jsonBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
jsonObject.toString());
Now create request
Request request = new Request.Builder()
.addHeader("header_a", "value_a") // to add header data
.post(formBody) // for form data
.post(jsonBody) // for json data
.post(multipartBody) // for multipart data
.build();
Response response = client.newCall(request).execute();
** fileToUpload is a object of type java File
** client is a object of type OkHttpClient
Maybe you mean this:
HttpUrl url = new HttpUrl.Builder().scheme("http").host(HOST).port(PORT)
.addPathSegment("xxx").addPathSegment("xxx")
.addQueryParameter("id", "xxx")
.addQueryParameter("language", "xxx").build();
You can use this lib: https://github.com/square/mimecraft:
FormEncoding fe = new FormEncoding.Builder()
.add("name", "Lorem Ipsum")
.add("occupation", "Filler Text")
.build();
Multipart content:
Multipart m = new Multipart.Builder()
.addPart(new Part.Builder()
.contentType("image/png")
.body(new File("/foo/bar/baz.png"))
.build())
.addPart(new Part.Builder()
.contentType("text/plain")
.body("The quick brown fox jumps over the lazy dog.")
.build())
.build();
See here:
How to use OKHTTP to make a post request?