LIB OKHTTP How to sent value null into $_POST? - android

hollow
i want sent valuse null into PHP. than php get $_POST
My code.
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
// post request
.addFormDataPart("test", null)
.build();
Request request = new Request.Builder()
.url(localhost/testnull.php)
.post(requestBody)
.build();
OK HTTP can't sent null. it's error.
thk every body

Related

Prevent OkHTTP from URL encoding RequestBody

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();

Android-REST API request :can't read custom header parameters in server

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

how to pass web api(post request) url parameters as requestbody in android using okhttp

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();

OkHTTP how to replace team_number from url POST

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();

Android OKHttp adding params

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?

Categories

Resources