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

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

Related

OkHttp - Adding attributes to body

Does anyone have experience with the OkHttp websocket library? I'm creating a simple chatroom app and my current issue is when my android client connects to the websocket server, it only gives the client's connection id. I'm trying add attributes to the body of the request, but I can't seem to get it working with OkHttp.
RequestBody requestBody = new FormBody.Builder()
.add("room_id", "e9502c54-927c-4639-a94f-8d03149c9c62")
.build();```
Request request = new Request.Builder()
.url("wss://mywebsocketurl.com")
.method("POST", requestBody)
.build();
Request request = new Request.Builder()
.url("wss://mywebsocketurl.com")
.post(requestBody)
.build();
I'm trying to add a room_id so when the user is successfully connected, it logs the connectionid+roomid to manage the chat rooms however I can't figure out how to add attributes to the body
Edit 1:
Request request = new Request.Builder()
.url("wss://mywebsocketurl.come")
.build();
EchoWebSocketListener listener = new EchoWebSocketListener();
ws = client.newWebSocket(request, listener);
This works when establishing the connection for the first time, but I can't seem to figure out how to add body attributes to this.

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

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

LIB OKHTTP How to sent value null into $_POST?

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

Categories

Resources