We have this code which we are using to post data from an Android app to a .Net Rest service. One of the fields that the backend is and array. Swagger specifies it as
modelbinding Array[integer]
How should we put the value array of integers in the urlParameters so we can post it?
String urlParameters = "field1=abc&field2=def";
URL url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
writer = new DataOutputStream (connection.getOutputStream ());
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(writer, "UTF-8"));
bufferedWriter.write(urlParameters);
bufferedWriter.flush ();
bufferedWriter.close ();
Using Json format is the best way to send data. you can convert your data in json array then json array to string. Now your string(represented in json formate) can easily be sent.
Related
I'm having problems doing a Postman REST call copying a REST call in Java.
I tried to set request properties on Postman the same way they're set in Java, but it's not working.
I have to send a base64 string with this call (i put in italic the code line where this is done in Java code)
URLConnection connection = new URL(url + content).openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setConnectTimeout(timeout);
((HttpURLConnection) connection).setRequestMethod("POST");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
OutputStream output = connection.getOutputStream();
JSONObject conf = new JSONObject();
conf.put("signedEvidence", String.format("%s", baos));
*output.write(conf.toString().getBytes());*
output.flush();
checkHttpStatus(connection);
I configured Postman like this:
And i receive this answer:
EDIT - In few words: the REST call works fine in Java, but i need to do some of these calls in Postman with my own variable (the service i'm calling do some works with base64 string i pass him).
EDIT2 - Main problem, in my opinion, is the line:
output.write(conf.toString().getBytes());
which set the base64 in my Java call, and i don't understand/know how to do the same in my Postman call.
Try only adding the following values:
Then, add the content type and the values which you need to pass.
I am trying to create an Asset on Azure Media Services using REST API from Android. I am following this documentation and this is my code to connect with AMS endpoint from Android,
urlConnection = (HttpURLConnection) this._url.openConnection();
urlConnection.setRequestProperty("Content-Type", String.valueOf("application/json"));
urlConnection.setRequestProperty("DataServiceVersion", String.valueOf("1.0;NetFx"));
urlConnection.setRequestProperty("MaxDataServiceVersion", String.valueOf("3.0;NetFx"));
urlConnection.setRequestProperty("Accept", String.valueOf("application/json"));
urlConnection.setRequestProperty("Accept-Charset", String.valueOf("UTF-8"));
urlConnection.setRequestProperty("Authorization", String.format(Locale.ENGLISH, "Bearer %s", _token));
urlConnection.setRequestProperty("x-ms-version", String.valueOf(2.11));
urlConnection.setRequestProperty("x-ms-client-request-id", UUID.randomUUID().toString());
urlConnection.setRequestProperty("Host", this._host);
urlConnection.setRequestProperty("Content-Length", String.valueOf(this._postData.length));
urlConnection.setRequestProperty("Expect", String.valueOf("100-continue"));
urlConnection.setConnectTimeout(CONNECTION_TIME_OUT);
urlConnection.setReadTimeout(CONNECTION_READ_TIME_OUT);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
OutputStream wr= urlConnection.getOutputStream();
wr.write(_postData);
wr.flush();
wr.close();
And my _postData variable is byte array which I am converting from json,
_fileName = _fileName.replace(" ", "-");
JSONObject jo = new JSONObject();
jo.put("Name", _fileName+".mp4");
jo.put("Options", String.valueOf(0));
this._postData = jo.toString().getBytes("UTF-8");
I have tried using Google Chrome's REST api client extension to check the post request and It works fine. I got the response I was expecting from chrome's REST api client extension but using Android, I am not getting the same response. I am getting this response from this code which is the step I have already performed before running this code. I have used both endpoints https://media.windows.net/ and https://wamsbayclus001rest-hs.cloudapp.net/ but it is not working from Android. I believe that Android is changing Headers or something is wrong with headers that AMS not parsing properly. Can anyone guide me how to achieve this using Android HttpUrlConnection?
Thanks.
I fixed it by changing OutputStream to DataOutputStream
DataOutputStream wrr = new DataOutputStream(urlConnection.getOutputStream());
wrr.write(this._postData);
wrr.flush();
wrr.close();
and changed my json array to string,
String s = "{\"Name\":\"" + _fileName + ".mp4\", \"Options\":\"0\"}";
and it worked fine.
We are sending HTTPURLRequest to server.
When we are sending English content its working fine.But, when we are sending Arabic language content we are getting
Server returned HTTP response code: 500
We had written below code
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(SendRequest.getBytes().length));
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
dataout.writeBytes(SendRequest);
dataout.flush();
dataout.close();
BufferedReader bufferreader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
When I use connection.getInputStream() I am getting 500 error
We are using utf-8 also.But, still getting the error
can any one help me
You can use a library to escape the special chars:
StringEscapeUtils.escapeJava("هولاء كومو")
This class is available on: Commons Lang from Apache
Hope this helps!
Check the HTTP Status Response Codes. An error happened on the server, so the diagnostics will need to be performed on the server, not on the client.
I'm using jsoup for getting information from different API and parsing it.
Functional compounds enough for all occasions, as long as I did not need to implement something in the likeness of the following code..
URL obj = new URL(url);
javax.net.ssl.HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
String postParams = new StringBuffer("{\"method\": \"getAccountInfo\",
\"params\": [], \"id\": 1}");
conn.setDoOutput(true);
// Send post request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(postParams.toString());
wr.flush();
wr.close();
I spent a lot of searching and re-read all the available documentation, but have not found a method to insert in the post parameters JSONArray as in the example.
Maybe I'm missing something, I will be very grateful for the help, how to implement the code using org.jsoup.HttpConnection.
I am working on a java app, to upload images on yfrog.com.
I can post on the API page successfully but without binary files just with a string parameters.
Also the method I use only accept "String".
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.write(data); accept String only.
I tried to put the image path but it doesn't work.
Do yourself a favor and take a look at Apache HttpComponents.