Request with JSON body and double header entries - android

I have this request on Postman :
How can i make this request on Android?

You ca use very convenient service jsonschematopojo.org which will create java objects feom your Rest response and retrofit library. See post How to consume this JSON structure via Retrofit 2?

Related

how to send retrofit request body from postman when request body is just json

I'm send data to rest service using retrofit and it works but when server crash and i need to test the api in postman but when using postman data is null,i can see that data are delivered using dump and die but when trying to access it return null
#POST("adToCart")
Call<CartDataResponse> addToCart(#Body CartData cartData);
As your request body is CartData, you can just simply use Gson.toJson(cartData) where, if you're using google Gson libray with retrofit and cartData is an object of CartData.java or the request model class. Print that value of the json which you have got from Gson.toJson(cartData), copy the json and put it into the body portion of postman and make sure that you have fill the hearder and correct endpoint, Then hit 'send' button which will give you your expected response. If not please share your steps for confirmation.

403 on Android for Reddit API but not on browser

When making a request with Retrofit using an OKHttp Client with interceptors to add headers:
(Authorization: Bearer + access_token)
(User-Agent: "user_agetn_as_described_by_reddit")
Using the oauth.reddit.com/.json URL as recommended by Reddit, I get a 403. Now, when I do this same thing on Postman or something similar, I get a 200 and the expected JSON.
Is anyone aware of something related to Android or Retrofit or OKHttp that could be causing me this pain?
I was using the auth code instead of the access_token to make my requests. A mistake from my side. You should use the auth code to make a token request.

How to send json to http url connection android

I have HTTP path that need to send JSON
{
"domain": "mas.org.il",
"params": {
"type":"hug"
}
}
to get another JSON with all items to insert into my Android app
How do I send this JSON to http URL and get JSON to parse it into my app.
please read this useful document to know how can you implementing this feature
https://www.itsalif.info/content/android-volley-tutorial-http-get-post-put
Just to give a vary basic idea,add the json in apache NameValue pair or use apache Entitys with HttpPost .. or you can follow this.
How To Send json Object to the server from my android app

Retrofit dynamic HTTP methods

I'm planning to replace Apache HTTP client with retrofit in my project.
The problem I'm facing is that retrofit didn't(I couldn't find) support setting HTTP method to request at runtime.
In my Web Service I don't know what HTTP method to call in advance, so annotations #GET, #POST, ... are useless.
Retrofit is not designed for dynamic url requests. You configure retrofit with your API base URL then make specific requests.
For a more flexible dynamic option use out OkHttp. It is the HTTP Client used by Retrofit and it easy to work with.
You can use Retrofit 2 for dynamic URL request with the new #Url annotation:
public interface CarService {
#GET
public Call<ImageResponse> getPicture(#Url String url);
}
Then just also create #POST, #PUT etc. You are going to have to make the choice somewhere.

Android Calling Django Server With POST Data

someone may asked my question already but I cannot find any suggestions.
I writing an Android app which needs to access my Django server by using HttpsURLConnection then Django server will return a JSON array to Android.
The view function in Django will receive the parameters from request.POST and generate the JSON array then return using HTTPResponse Django method. It does not need any Templates and Forms.
When I call the Django view function from Android, it returns 403 error. I know that it is because the POST data does not contains "csrf_token".
My problem is: How can I get the "csrf_token" and put it into my POST data before I send it to Django? I try disable the CSRF checking by "#csrf_exempt" it can return the correct result to Android app but I would not disable the CSRF checking.
Thanks,
Wilson
You have to send the cookies and also have to send a header 'X-CSRFToken' with csrftoken.
This is what I do (may not be the best way):
Get csrf token via a get request.But first try to see if you get a csrftoken cookie by doing same request on your browser with developer tools. If not, you should use ensure_csrf_cookie decorator
from django.views.decorators.csrf import ensure_csrf_cookie
#ensure_csrf_cookie
def your_view(request):
pass
Now using the same HttpUrlConnection object do this :
String cookieString="";
String csrftoken="";
// The below code can be shortened using for-each loop
List<HttpCookie> cookies=cookieManager.getCookieStore().getCookies();
Iterator<HttpCookie> cookieIterator=cookies.iterator();
while(cookieIterator.hasNext()){
HttpCookie cookie=cookieIterator.next();
cookieString+=cookie.getName()+"="+cookie.getValue()+";";
if(cookie.getName().equals("csrftoken")){
csrftoken=cookie.getValue();
}
}
Add the following to your post request:
urlConnection.setRequestProperty("X-CSRFToken", csrftoken);
urlConnection.setRequestProperty("Cookie", cookieString);

Categories

Resources