I have the following { #POST("mobile/posts/add")
Call publish(#QueryMap Map options}
Where one of the options is an encoded based64 string from bitmap image. The server side takes it as a query parameter, but retrofit keeps saying bad request. I have seen some solutions with the multipart, but those ones are not working for me. The server side takes only string query parameters, thus the post should only be sent as #query or #queryMap.
Thanks in advance
Related
I'm getting an Api response as this
X-Amz-Credential=0XCA1HQW6NU67Z1FP3U1%2F20221011%2Fus-east-1%2Fs3%2Faws4_request
But when I send this value as query parameters in the next Api, It is automatically converted into
X-Amz-Credential=0XCA1HQW6NU67Z1FP3U1%252F20221011%252Fus-east-1%252Fs3%252Faws4_request
wherever there is %2F it is converted into %252F, So the Api is failing as 400 Bad request.
You can use the encoded property of retrofit's #Query annotation to mark the value as already encoded, preventing it from being URL
encoded again.
For example:
#Query(value = "credential", encoded = true) credential: String
I think you can fix it by first decoding the original response and then sending it as a query param to next request. You can use one of the below methods for decoding:
import android.net.Uri
Uri.decode(response)
OR
import java.net.URLDecoder
URLDecoder.decode(response, "utf-8")
For response = 0XCA1HQW6NU67Z1FP3U1%2F20221011%2Fus-east-1%2Fs3%2Faws4_request,
the decoded response will be 0XCA1HQW6NU67Z1FP3U1/20221011/us-east-1/s3/aws4_request
I have a string like "key=value" which I want to provide in my REST POST requests body. e.g.:
String content = "key=value";
Once set and trasmitted my logout output tells me, that my '=' sign content was converted to a '\u003d' wihch results in "key\u003dvalue" in my REST POST request content. Then the server responds a 400 Bad Request..
How can I prevent the '=' being converted to that unicode ?
Try this :
String content = "key\=value";
My Android app is making two GET calls to my Server API. In the first one, is this, where parameter code is a 256 char String.
$.getJSON( myServerEndpoint, {
action: "doStuff1",
username: $("#username").val(),
code: my256charString,
format: "json"
})
.done(function( data ) {
doStuff2Response(data);
});
The second one is this, where parameter code is a 5120 char String. Both reach the same server endpoint.
$.getJSON( myServerEndpoint, {
action: "doStuff2",
username: $("#username").val(),
code: my5120CharString,
format: "json"
})
.done(function( data ) {
doStuff2Response(data);
});
When I call both of them from the same device and same user connected to WiFi or most mobile data providers, it works perfectly.
However, when I connect from a Vodafone data connection, the second request never reaches the server. I cannot find any other explanation than that there is a limit on the length of the parameters with Vodafone.
Any ideas or solutions?
OK, so here it goes. First, read this: What is the maximum length of a URL in different browsers?
Yes, there's a limit in the length of the "URL", but someway I don't know how to explain why it is happening only for vodafone. Plus, I don't even know how the request pass through their servers anyways.
As for the solution, you should consider changing from GET request to POST request when the payload is too big.
Quick solution: Base64-encode the code part of the message. Downside: you must decode on the server. This is a standard function in most languages though.
If you're already using Base64 or somesuch cypher, what about Blobs? https://developer.mozilla.org/en-US/docs/Web/API/Blob
chromano's suggestion is spot-on too, just switch to POST and you will definitely get an unlimited Post Body. Downside: Have to JSON.stringify and JSON.parse for yourself, and if you want to expose this URL to a user (say as a link to share) it now can't carry the same information (URL's are GET requests).
iam calling a webservice(php http get request)
http://website.com/admin/employee_login.php?fun_name=abc&company_code=1&employee_ss=123>ime=02:22 PM&x_id=350&v_id=9&task={"Walking":"1","Transfers":"1","OstomyCare":"1"}
but when am encoding this url am getting something different with %20 something like that, and it's not updating the tasks in the server database. Is there any method where i can pass parameters as a json array without encoding like same above ?
thank you
Before sending the url and parameters are url encoded. For instance spaces become %20. On the receiving side php should url decode the parameters. There is a php function which does that for you. After that you have the original parameters back.
use:
String value = "my url value"; // put here your url
URLEncoder.encode(value,"UTF-8")
I am new t ajax, but quite familiar with android. I am converting a ajax program to android app. As a part of it, i need to post data to the server. Below is the given post command in ajax.
var postTo = 'xyz.php';
$.post(postTo,{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' } ,
function(data) {
if(data.success){
window.localStorage["sa_id"] = data.mid;
window.location="getempdb.html";
}
if(data.message) {
$('#output').html(data.message);
} else {
$('#output').html('Could not connect');
}
},'json');
I want to implement this in android but under very little from the above statements. Could anyone who is good at ajax help me out with this thing. As of now, i get the user name and telephone number as a edit text input. I need to send this to php using http client. I know how to send data using php, but do not know what format to send and whether its a string to send or as a json object to send. Please help in interpreting the above code and oblige.
Apparently, this uses UrlEncodedFormEntity if you are using HttpClient in android.
This is created by using a List of NameValuePair.
from the parameters to the $.post:
{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' }
You have to create a NameValuePair for employee_name, one for phone ... each of which is fetched from a HTML element name employee_name, phone ... This is where you put the values from your EditTexts.
It returns a JSON formatted String, which you have to parse (typically using JSONObject obj = new JSONObject(result); once you have fetched the result from the server)
In this JSON object, you have a key named success, which format is not specified, except you can assume things went well if it is present ; a key mid, and a key message.