I am using the following code to execute a HTTP POST towards an external system. The problem is that the external system always gets a 'null' content type when using the code below. Is there a way to set the contenttype when using HTTPBuilder.
I tried other tools that execute the same request but then the remote system gets a good contentType ('application/json').
def execute(String baseUrl, String path, Map requestHeaders=[:], Map query=[:], method = Method.POST) {
try {
def http = new HTTPBuilder(baseUrl)
def result = null
// perform a ${method} request, expecting TEXT response
http.request(method, ContentType.JSON) {
uri.path = path
uri.query = query
// add possible headers
requestHeaders.each { key, value ->
headers."${key}" = "${value}"
}
// response handler for a success response code
response.success = { resp, reader ->
result = reader.getText()
}
}
return result
} catch (groovyx.net.http.HttpResponseException ex) {
ex.printStackTrace()
return null
} catch (java.net.ConnectException ex) {
ex.printStackTrace()
return null
}
}
Adding a specific header to the request seems to solve my problem.
def execute(String baseUrl, String path, Map requestHeaders=[:], Map query=[:], method = Method.POST) {
try {
def http = new HTTPBuilder(baseUrl)
def result = null
// perform a ${method} request, expecting TEXT response
http.request(method, ContentType.JSON) {
uri.path = path
uri.query = query
headers.'Content-Type' = 'application/json'
// add possible headers
requestHeaders.each { key, value ->
headers."${key}" = "${value}"
}
// response handler for a success response code
response.success = { resp, reader ->
result = reader.getText()
}
}
return result
} catch (groovyx.net.http.HttpResponseException ex) {
ex.printStackTrace()
return null
} catch (java.net.ConnectException ex) {
ex.printStackTrace()
return null
}
}
Try setting the requestContentType in the body of your request block...
http.request(method, ContentType.JSON) {
uri.path = path
uri.query = query
requestContentType = groovyx.net.http.ContentType.URLENC
.......
}
Related
I'm trying to do a post request in Android Studio written in Kotlin
I'm posting a JSON object to our server and then the server is returning a JSON object back. But what I'm doing here is decoding the response body as a string and then converting it into the data structure we need. I'm sure there is a better and simpler way to do what I need done.
My current code works but the major issue I'm having is formatting the string if our objects have nested objects which is why I want to figure out a better way to turn the response body into a json object.
I'm not too familiar with many request libraries for kotlin but I have looked into okhttp3 but I'm not sure how to post a json object, attach headers and decode the response body into a json object.
I know for okhttp3 I need to convert the json object to a string to post other than that I'm lost.
Breakdown of what's needed:
Post JSON Object To Server
Send Headers With Post Request
Decode Response Body into JSON Object/ Kotlin Equivalent
Simplify What I'm Trying to Do if Possible
This is the current code I have
private fun postRequestToGetDashboardData() {
val r = JSONObject()
r.put("uid", muid)
r.put("token", mtoken)
SendJsonDataToServer().execute(r.toString());
}
inner class SendJsonDataToServer :
AsyncTask<String?, String?, String?>() {
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
if (result.equals(null)) {
val t = Toast.makeText(this#Home, "No devices to display", Toast.LENGTH_LONG)
t.setGravity(Gravity.CENTER, 0, 0)
t.show()
} else {
intentForUnique.putExtra("FirstEndpointData", result)
var list = handleJson(result)
adapter.submitList(list)
dashboardItem_list.adapter = adapter
adapter.notifyDataSetChanged();
dashboardItem_list.smoothScrollToPosition(0);
}
}
override fun doInBackground(vararg params: String?): String? {
val JsonDATA = params[0]!!
var urlConnection: HttpURLConnection? = null
var reader: BufferedReader? = null
try {
val url = URL("URL");
urlConnection = url.openConnection() as HttpURLConnection;
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Authorization", mtoken);
urlConnection.setRequestProperty("Accept", "application/json");
val writer: Writer =
BufferedWriter(OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
writer.write(JsonDATA);
writer.close();
val inputStream: InputStream = urlConnection.getInputStream();
if (inputStream == null) {
return null;
}
reader = BufferedReader(InputStreamReader(inputStream))
var inputLine: String? = reader.readLine()
if (inputLine.equals("null")) {
return null
} else {
return inputLine
}
} catch (ex: Exception) {
Log.e(TAG, "Connection Failed", ex);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (ex: Exception) {
Log.e(TAG, "Error closing stream", ex);
}
}
}
return null
}
}
private fun handleJson(jsonString: String?): ArrayList<SensorData> {
val jsonArray = JSONArray(jsonString)
val list = ArrayList<SensorData>()
var x = 0
while (x < jsonArray.length()) {
val jsonObject = jsonArray.getJSONObject(x)
list.add(
SensorData(
jsonObject.getInt("deviceId"),
// jsonObject.getString("deviceName"),
jsonObject.getInt("battery"),
jsonObject.getString("dateTime"),
jsonObject.getInt("airValue"),
jsonObject.getInt("waterValue"),
jsonObject.getInt("soilMoistureValue"),
jsonObject.getInt("soilMoisturePercent")
)
)
x++
}
return list
}
So the json data being returned back is an array of this structure (our backend is written in Go)
type Device struct {
DeviceID int `bson:"deviceId" json:"deviceId"`
Battery int `bson:"battery" json:"battery"`
DateTime time.Time `bson:"dateTime" json:"dateTime"`
AirValue int `bson:"airValue" json:"airValue"`
WaterValue int `bson:"waterValue" json:"waterValue"`
SoilMoistureValue int `bson:"soilMoistureValue" json:"soilMoistureValue"`
SoilMoisturePercent int `bson:"soilMoisturePercent" json:"soilMoisturePercent"`
}
Having a few issues with the response handling of my httpbuilder post and json
within my service I have:
def jsonDataToPost = '{"accountNumber" : ' + accNo + ',"accountName" : ' + accName + '}'
def http = new HTTPBuilder('https://myurl.com/dataInput')
def jsonResponse
http.auth.basic ('username','password')
http.request(POST, ContentType.JSON) {
headers.'Content-Type' = 'application/json'
body = jsonDataToPost
response.success = { json ->
println("Success")
jsonResponse = json
}
response.failure = { json ->
println("Fail")
jsonResponse = json
}
}
firstly for some reason the code actually skips out rather than completing and so I'm not getting the jsonReponse I'm after but I can't figure out why? If I reponse my response.success/fail and I post correct data my json post works but again I still get no json back
Try this,
def requestData = [foo:bar]
http.request(POST, ContentType.JSON) {
headers.'Content-Type' = 'application/json'
body = (requestData as JSON).toString()
response.success = { resp, reader ->
println("Success")
jsonReponse = reader.text
}
response.failure = { resp, reader ->
println("Failed, status: " + resp.status)
jsonReponse = reader.text
}
}
If you're using Grails 3.x you can use http-builder-ng
https://http-builder-ng.github.io/http-builder-ng
Clarification: POST requests are between different websites or different controller in the same project.
I want to POST Json data to another action method. I am using Newtonsoft for json serialization, the thing is that in JsonMethod, the field Name comes with null. Am I missing something?
My model:
class Person
{
[JsonProperty("name")]
public string Name{get;set;}
}
The action method that post the json:
public ActionResult Method1()
{
Person p = new Person(){Name = "Test"}
string urlToRedirect = "..urlRoute../JsonMethod";
var res = SendRequest(urlToRedirect, p);
//...do anything with res
}
The method that receive the Model
[HttpPost]
public ActionResult JsonMethod(Person p)
{
if(p.Name == "Test")
return Json("ok");
else return Json("bad");
}
The method that send the request
public async bool SendRequestAsync(string requestUrl, object data)
{
string json = JsonConvert.SerializeObject(obj, Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
if (request != null)
{
request.Accept = "application/json";
request.ContentType = "application/json";
request.Method = "POST";
using (var stream = new StreamWriter(await request.GetRequestStreamAsync()))
{
stream.Write(json);
}
using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)
{
if (response != null && response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
if (response != null)
{
Stream responseStream = response.GetResponseStream();
//return true or false depending on the ok
return GetResponseModel(responseStream);
}
}
}
}
catch (WebException ex)
{
var response = ex.Response;
Stream respStream = response.GetResponseStream();
//return true or false depending on the ok
return GetResponseModel(respStream);
}
catch (Exception e)
{
return false;
}
return false;
}
My mistake was the "name" attribute property on person, it must be with capital Letter "Name". Thats why it was coming with null.
[JsonProperty("Name")]
i'm trying to make an async call to a domain. The following code works well if i specify a valid address with json response, but when the address is not valid, i want to be able to catch any possible exceptions.
How can i catch the returned exception?
Here an extract from stacktrace:
Message: Invalid JSON String
...
http.AsyncHTTPBuilder - Exception thrown from response delegate:
groovyx.net.http.HTTPBuilder$RequestConfigDelegate#420db81e
Here the code:
def http = new AsyncHTTPBuilder( poolSize : 1,
contentType : ContentType.JSON )
def futureResult
futureResult = http.request( "http://www.notexistingdomainxyzwq.com/",
Method.GET,
ContentType.JSON ) {
response.success = { resp, json ->
log.info("SUCCESS")
}
response.failure = { resp, json ->
log.info("ERROR")
}
}
log.info("Call started");
try {
while (!futureResult.done) {
log.info('waiting...')
log.info("DONE: ${futureResult.done}")
Thread.sleep(1000)
}
} catch(ex) {
log.error("EXCE ${ex}")
}
log.info("Call completed")
If you call futureResult.get() to block and wait for the result, this will throw the exception which you can catch:
try {
def result = futureResult.get()
log.info( "Done: $result" )
} catch(ex) {
log.error("EXCE ${ex}")
}
I'm trying to see if a specific string exists in an html page but I can't seem to find an easy way to get the string that represents the body.
I've attempted:
http.request(Method.GET, { req ->
uri.path = '/x/app/main'
response.success = { resp, reader ->
assert resp.status == 200
println reader.text.startsWith('denied')
}
response.failure = { resp ->
fail("Failure reported: ${resp.statusLine}")
}
})
but reader.text is a NodeChildren object.
How do I get the html (or more specifically, the contexts of the body) as a string?
You can get an input stream directly off of the response. Try this:
http.request(Method.GET, { req ->
uri.path = '/x/app/main'
response.success = { resp ->
assert resp.status == 200
println resp.entity.content.text.startsWith('denied')
}
response.failure = { resp ->
fail("Failure reported: ${resp.statusLine}")
}
})