Android Question post json error

red30

Well-Known Member
Licensed User
Longtime User
I am trying to send a json file, but I get an error: “ResponseError. Reason: Bad Request, Response: <h1> Bad Request (400) </h1> "
Moreover, if I send the same file via postman, everything comes up fine.
jtest.png

Here is my code:
B4X:
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "data"
    fd.Dir = File.DirInternal
    fd.FileName = "1.json"
    fd.ContentType = "application/json"
    j.PostMultipart("link", CreateMap("data": "1.json"), Array(fd))
    j.GetRequest.SetContentType("multipart/form-data")
What am I doing wrong?
 

red30

Well-Known Member
Licensed User
Longtime User
Maybe it will be clearer this way:
B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    Dim basicAuth As String = "username:password"
    Dim su As StringUtils
    Dim authInfo = su.EncodeBase64(basicAuth.GetBytes("UTF8"))
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "data"
    fd.Dir = File.DirInternal
    fd.FileName = "1.json"
    fd.ContentType = "application/json"
    j.PostMultipart(link, CreateMap("data": "1.json"), Array(fd))
    
    j.GetRequest.SetContentType("multipart/form-data")
    j.GetRequest.SetHeader("Cookie", "csrftoken=XXX")
    j.GetRequest.SetHeader("Authorization", "Basic "&authInfo)
    
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release
Here is the code from the postman (Generated code for Java - OkHttp ):
Java:
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
  .addFormDataPart("data ","/C:/1.json",
    RequestBody.create(MediaType.parse("application/octet-stream"),
    new File("/C:/1.json")))
  .build();
Request request = new Request.Builder()
  .url(link)
  .method("POST", body)
  .addHeader("Authorization", "Basic XXX")
  .addHeader("Cookie", "csrftoken=XXX")
  .build();
Response response = client.newCall(request).execute();
I cannot show the data because the server is private ...
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
One more question. I just copied the csrftoken value from postman. Can I get it in the same way as Basic for Authorization?
B4X:
    Dim basicAuth As String = "username:password"
    Dim su As StringUtils
    Dim authInfo = su.EncodeBase64(basicAuth.GetBytes("UTF8"))
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Maybe change this?
B4X:
fd.ContentType = "text/plain"
I tried, same error:
ResponseError. Reason: Bad Request, Response: <h1>Bad Request (400)</h1>
Moreover, it is clear that the data is being transmitted, but something is wrong with the format ... Because if the request is not correct at all, for example, I do not specify the login and password, or I specify with an error in response, I get:
<h1>Server Error (500)</h1>

I should get "400" if the json is not valid. This is stated in the documentation. But if I manually pass the same json through the postman, I get "status": "success" in return. I came to the conclusion that the json is correct, but there is something wrong with the transfer format ...
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
The path seems different too. Try add the C:/ also.
The path is also just an example. And as I said earlier on the PC via postman, everything works fine. The problem is only from the android side.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You can use this site https://ptsv2.com/ to test both post methods (Postman and B4A) and see what the difference is
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Can you post a link to the documentation? Or telling us which api you are using.

Maybe you just need to use job.PostBytes and post the jsoncontent this way.
I cannot show the documentation as it is private, just like the server. Here is some of what she has written:
Json is formed and sent by POST method to url
Parameter -> format-data
key: data | value: load json file.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I can give an example php from postman (instead of Java - OkHttp) sending a request. Maybe this will clarify better why everything works in postman, but does not work in android?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Can you give an example of how to use this site?
Create a new random toilet. That should give you an url you can use in your examples. Use the same url for the Postman, PHP and B4A examples and check the "toilet" to see what they post. Note the differences and adjust your code.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Create a new random toilet. That should give you an url you can use in your examples. Use the same url for the Postman, PHP and B4A examples and check the "toilet" to see what they post. Note the differences and adjust your code.
I created a random "toilet". But when I try to send json through android, it gives me an error:
ResponseError. Reason: , Response: Error parsing multipart data
Moreover, if I send via postman, everything is sent fine.
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Try to send the file content instead of the file name and remove the MultipartFileData.
I do not understand ... Can you give an example?
B4X:
    Dim JSON As JSONParser
    JSON.Initialize(File.ReadString(File.DirInternal,"1.json"))
    map2=JSON.NextObject
    j.PostMultipart(link, map2, Null)
Like this?
Or
B4X:
map2.Put("data",File.ReadString(File.DirInternal,"1.json"))
j.PostMultipart(link, map2, Null)
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I meant to the second option.
If I use the server I need, I get the same "Bad Request (400)" error
If I use "toilet", then again the error "ResponseError. Reason:, Response: Error parsing multipart data "
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Is this an online server? Where is its documentation?
Yes, the server is online. I can send json to it via postman.
Part of the documentation:
Json is formed and sent by POST method to url -> XXX
Parameter -> format-data
key: data | value: load json file.
If json is not valid, then you will receive a response
400 BAD REQUEST
 
Last edited:
Upvote 0
Top