{
"From":"donotreply#xxx.com", "To":["abc#xxx.com"],
"Cc":["abc#xxx.com"], "Bcc":["abc#xxx.com"],
"Subject":"Export", "TemplateName":"Agent",
"EmailType":"Agent", "Attachments":[{
"Name":"wifi.txt", "Type":"txt",
"StreamData":{"NzI5OTk4NzgxNg0KDQoxIG0tNjczDQoNCjJtLTExMjINCjRtLTE1NzENCjgtMjE4Mw0KDQoxMDYxDQoNCjUwMCBtb2RlbSByZWZ1bmRhYmxl"}}]` ``
}`input json``
I'm calling one method by passing this value as input json using postman, but the StreamDate value alone coming as null.
Can someone please help me on this?
Well it's an invalid JSON object.
StreamData has to be either a string or an object with a key.
Try
{
"From":"donotreply#xxx.com",
"To":["abc#xxx.com"],
"Cc":["abc#xxx.com"],
"Bcc":["abc#xxx.com"],
"Subject":"Export",
"TemplateName":"Agent",
"EmailType":"Agent",
"Attachments":[{
"Name":"wifi.txt",
"Type":"txt",
"StreamData": "NzI5OTk4NzgxNg0KDQoxIG0tNjczDQoNCjJtLTExMjINCjRtLTE1NzENCjgtMjE4Mw0KDQoxMDYxDQoNCjUwMCBtb2RlbSByZWZ1bmRhYmxl"
}]
}
In the sending code Base64 encode the byte array into a string.
Related
I'm trying to call an endpoint that accepts PUT requests and expects to be passed 3 different MultipartFile paramters. Let's call them A, B and C.
When I make a request to the same enpoint from Postman it works as intended. When I do it via the the reactor-netty lib I get back Error 400 Bad Request:
"Required request part 'A' is not present"
HttpClient
.create()
// skipping baseUrl and headers headers
.put()
.uri(ENDPOINT_URI)
.sendForm((req, form) -> form
.multipart(true)
.file("A", FILE_A, "application/json)
.file("B", FILE_B, "application/json)
.file("C", FILE_C, "application/json))
.response()
I could not find much info online to establish if this is the best way to achieve what I need. Can you please point me to where I'm going wrong or perhaps towards an alternative solution?
Thanks
After looking throught the source of the HttpClientForm (the class in which .file is called) I found this:
default HttpClientForm file(String name, InputStream stream, #Nullable String contentType) {
return file(name, "", stream, contentType);
}
as well as this:
default HttpClientForm file(String name, File file, #Nullable String contentType) {
return file(name, file.getName(), file, contentType);
}
Somehow I thought that the first paramter 'name' is the one that is matched with the #RequestParam value. By the looks of it its actually the second.
Also if using an input stream instead of a File I had to call the the file method with 4 paramters and pass the name explicitly as the second parameter like so:
file(name, "A", stream, contentType)
I have a webapi controller code looks like:
[HttpPost]
public HttpResponseMessage DownloadForms(FormCriteria criteria)
{
string downloadData = new FormsToCsvHelper(Umbraco, criteria).GetCsv();
return Request.CreateResponse(HttpStatusCode.OK, downloadData);
}
If i look at the data returned in debugger just before sending it back it looks like:
Created Date,IP,Form Name,Email address,Message,Full Name
31/05/2019 10:43:08,127.0.0.1,Contact form - test,test12#hotmail.com,test,Ismail Mayat
If I copy and paste it into file and save as csv it looks fine in excel.
However the data i actually get back looks like:
"Created Date,IP,Form Name,Email address,Message,Full Name\r\n31/05/2019 10:43:08,127.0.0.1,Contact form - test,test12#hotmail.com,test,Ismail Mayat"
The whole record set it wrapped in quote and the line feed is seen as a literal so when opening file in excel its all on one line.
There is a DelegatingHandler that fire but that is for a specific url request so for this request it does not do anything.
Anyone any ideas whats going on?
WebAPI will serialize the value as JSON by default, so that's why your value is enclosed in double quotes.
To get around this, you can use the StringContent class:
return new HttpResponseMessage(statusCode) {
Content = new StringContent(downloadData, Encoding.UTF8, "text/csv")
};
Hi I am using loadJSONObject function in Processing IDE to receive a JSON Object from a URL.
Following is my code:
JSONObject jsonUserLocations = loadJSONObject("http://smrt.utd.sg/eLocation/getLocs.php?userId="+usrID+"&sTime="+strtTime+"&eTime="+endTym);
This code works when the php returns some data (for some users).
The problem occurs when it doesn't return any data. (php doesn't return any data when there is no data, browser shows a blank page. this means user has no location data)
At this instance, the Processing IDE gives me an error saying;
a jsonobject text must begin with {
My question is how can I handle empty JSON Object in this type of situation? I need to skip if this is empty and request data for next user. Your help is much appreciated.
Thanks,
Hasala
Edit:
This is sample json object I receive when there is data.
{"locations":[{"latitude":"1.3809274","longitude":"103.7654596","startTime":"1421918587868","duration":"0","accuracy":"30"},{"latitude":"1.3805307","longitude":"103.7661015","startTime":"1421941711737","duration":"0","accuracy":"45"},{"latitude":"1.3805304","longitude":"103.7660959","startTime":"1421942011727","duration":"0","accuracy":"45"},{"latitude":"1.3799822","longitude":"103.7658037","startTime":"1421942311835","duration":"0","accuracy":"82.5"}],"success":1}
You have two options:
You could read the String form the URL first, and if it's blank, don't bother with the parsing.
Or you could just catch the exception that Processing throws:
try{
JSONObject jsonUserLocations = loadJSONObject("http://smrt.utd.sg/eLocation/getLocs.php?userId="+usrID+"&sTime="+strtTime+"&eTime="+endTym);
}
catch(JSONException e){
e.printStackTrace();
//json was blank, do something else
}
I need to do some queries against my datastore in Java but I can't seem to get the parameters syntax right. I tried like this:
String params = "?Active=1";
String urlString = "https://api.parse.com/1/classes/Cars" + params;
Or as per the document here:
String params = "where={Active:1}";
But both ways generate an exception.
If I don't do the query and simply try to get all the objects with this request string:
String urlString = "https://api.parse.com/1/classes/Cars"
everything works fine. So the problem is definitely the params sequence. So is there a way to do Prase.com rest queries in Java?
EDIT: adding the exception string in response to a request from the first comment:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.parse.com/1/classes/Cars?where={Active:1}
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
I should also note that when I use the regular http syntax, as in
params = "?Dealer=asdf";
the query comes back with all the objects, as if the parameter wasn't there.
Here are a couple of working examples for the params string:
String params = "where={\"objectId\":\"ldl49l3kd98\"}";
String params = "where={\"CompanyName\":\"BMW\", \"Price\":{\"$gte\":29000,\"$lte\":49000}}";
And if you need non English characters, like I do, encode the param string like this:
params = URLEncoder.encode(params, "UTF-8");
I'm having a very strange problem. My JSON.parse does not seem to work. I tried using eval too but that didn't help either. Below is my code:
var responseDoc = xmlHttp.responseText;
document.getElementById("debug1").innerHTML=responseDoc;
var response = JSON.parse(responseDoc);
document.getElementById("debug2").innerHTML=response.category;
My responseDoc looks like this
{"id":null,"category":"dog","price":"4321","name":"new product 123","sku":"1234","success":true}
But response.category is "undefined". Any ideas why this is happening? I've spent hours on it but couldn't figure out. Thanks a lot!
*UPDATE*:
Removed stringify as some people suggested -> still not working.
If I tried the code below, I get "Uncaught SyntaxError: Unexpected token <" from the console:
var response = JSON.parse(xmlHttp.responseText);
*UPDATE 2 *:
Found the problem. It's because my responseDoc is getting an HTML Doc. Not a JSON Object. Not sure why that happens. This is the code where I process ajax request (I'm using jsp):
JSONObject result = new JSONObject();
result.put("success",true);
result.put("id",request.getParameter("id"));
result.put("name", request.getParameter("name"));
result.put("sku",request.getParameter("sku"));
result.put("price",request.getParameter("price"));
result.put("category",request.getParameter("category"));
out.print(result);
out.flush();
You don't need to stringify something that is already a string (xmlHttp.responseText). The stringify method should be used on a javascript object to serialize it into a JSON string. So get rid of this stringification and simply parse the JSON string that you already have (using the JSON.parse method):
var response = JSON.parse(responseDoc);
You don't need to stringify a string :
var response = JSON.parse(responseDoc);