I am trying to add the source URL on an existing resource but I can't get it to work.
This is what I have:
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("id", new StringBody("resource id",ContentType.TEXT_PLAIN))
.addPart("Source", new StringBody("www.google.com",ContentType.TEXT_PLAIN))
.build();
postRequest = new HttpPost(host+"/api/3/action/resource_update");
postRequest.setEntity(reqEntity);
postRequest.setHeader("X-CKAN-API-Key", "myApi");
Which gives me the following error:
"success": false, "error": {"__type": "Validation Error", "url": ["Missing value"]}
I can add a Source field when I upload a file, but it doesn't create a link for it. Maybe because I use ContentType.TEXT_PLAIN instead of something that will tell CKAN that this is a link?
Edit:
By source I mean the link on the resource information where it points to the webpage of the original data. When manually uploading a file you have that option, I just cant get it to work with the API.
The error is telling you that you've not specified the url value for the resource, which is necessary.
The content that you POST needs to be in JSON format, whereas you appear to be sending it as multipart form data.
The JSON keys you need to send for a resource are id, url, name, format etc. i.e. you need to set url rather than Source. You can see the existing keys by looking at /api/action/resource_show?id=xyz.
The normal way to use resource_update is to do resource_show first, edit the JSON with the new values, and then resource_update. That way you don't lose any other properties of the resource, such as format.
Related
I am using AWS Textract to OCR images and create a searchable PDF as outlined in this AWS blog post.
The basic request code looks like this:
AmazonTextractClientBuilder builder = AmazonTextractClientBuilder.standard();
DetectDocumentTextRequest request = new DetectDocumentTextRequest()
.withDocument(new Document()
.withBytes(imageBytes));
DetectDocumentTextResult result = client.detectDocumentText(request);
List<Block> blocks = result.getBlocks()
This works out great however I would also like to write out and keep the original response JSON that contains all the information on what was detected where etc.
Is there a way to get to the response JSON using the JAVA SDK?
AWS doesn't return the response JSON to you in raw form. The assumption may have been that it wouldn't be required once it has been converted to a DetectDocumentTextResult object.
You are able to convert the DetectDocumentTextResult object to JSON (example) which should provide identical values. Note that the variable names will not be identical (e.g.: DocumentMetadata vs documentMetadata) but the values of those variables will be the same.
In the app, I'm currently making, I need to read a CSV file from a downloadable link and show its data.
For example, consider this link: https://api.covid19india.org/csv/latest/case_time_series.csv
If you click on the link, it'll download a CSV file.
So, what I want to do is, when the user opens the app, I want to access the data in this CSV link, parse it and show the data on the screen in a recycler view.
How to do that?
I found this Kotlin code as one of the way to get the data in CSV by using Volley's stringRequest. In this case we get all the data as a string with rows being separated by \n and data in a row being separated by commas(,)
For this sample code, I'm accessing date from this URL: https://sample-videos.com/csv/Sample-Spreadsheet-10-rows.csv
val queue = Volley.newRequestQueue(this)
val url = "https://sample-videos.com/csv/Sample-Spreadsheet-10-rows.csv"
val stringRequest = StringRequest(
Request.Method.GET, url,
{ response ->
// Display the first 500 characters of the response string.
binding.csvDataTextView.text = "Response is: ${response.substring(0, 500)}"
val allLinesInResponse = response.substring(0)
var rowsData: List<String> = allLinesInResponse.split("\n")
Log.d("abc", "The Volley request worked")
},
{
Log.d("abc", "The Volley request didn't work!")
})
queue.add(stringRequest)
queue.start()
There may be other better ways, but this is one of those which work.
I know how to retrieve a file with a specified name like this:
HttpGet httpGet = new HttpGet(siteUrl +
"_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/Working%20Files/FolderName')/Files('"
+workbookName+"')/$value");
but is there a way to retrieve it without the name? maybe by index?
Ive checked the documentation on https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest but saw no other examples on how else to retrieve files
Every file you upload into the Sharepoint will have the field Id mapped to it. This ID would be returned as part of the response whenever you create the file in the Sharepoint. This response would also return several links and fields related to the particular file you uploaded, in order to perform any operations on that file.
Using these fields such as Id, E-Tag you should be able to form another request to get the file name.
You can also find the request URL in the response you received after file creation.
I asked this question before and Evgeniy Dorofeev answered it. Although worked for direct link only, but I accepted his answer. He just told me about check the content type from direct link:
String requestUrl = "https://dl-ssl.google.com/android/repository/android-14_r04.zip";
URL url = new URL(requestUrl);
URLConnection c = url.openConnection();
String contentType = c.getContentType();
As far I know, there are two URL types to download a file:
Direct link. For example: https://dl-ssl.google.com/android/repository/android-14_r04.zip. From this link, we can download data directly and get the file name, included with file extension (in this link, .zip extension). So we can know what file to be downloaded. You can try to download from that link.
Undirect link. For example: http://www.example.com/directory/download?file=52378. Have you ever tried to download data from Google Drive? When downloading data from Google Drive, it will gives you an undirect link, such as the link above. We never know whether the link contains a file or webpage. Also, we don't know the file name and file extension is, because of this link type is unclear and random.
I need to check whether it is a file or webpage. I must download it if the content type is a file.
So my question:
How do I check the content type from an undirect link?
As shown in the comments of this question, can HTTP-redirects solves the problem?
Thanks for your help.
After you open an URLConnection, a header file is returned. There are some information about the file in it. You can pull what you want from there. For example:
URLConnection u = url.openConnection();
long length = Long.parseLong(u.getHeaderField("Content-Length"));
String type = u.getHeaderField("Content-Type");
length is size of the file in bytes, type is something like application/x-dosexec or application/x-rar.
Such links redirect browsers to the actual content using HTTP redirects. To get the correct content type, all you have to do is tell HttpURLConnection to follow the redirects by setting setFollowRedirects() to true (documented here).
MimeTypeMap.getFileExtensionFromUrl(url)
This one worked for me, you have to use retrofit to check the headers of response. First you have to define an endpoint to call it with the url you want to check:
#GET
suspend fun getContentType(#Url url: String): Response<Unit>
Then you call it like this to get the content type header:
api.getContentType(url).headers()["content-type"]
I'm using the following code to upload a file onto Alfresco
CloseableHttpClient client=HttpClients.createDefault();
HttpPost postMethod=new HttpPost(AlfrescoRequests.getUploadRequest()+"?alf_ticket="+DocumentUno.alFrescoSessionTicket.replace("\"", ""));
System.out.println(AlfrescoRequests.getUploadRequest()+"?ticket="+DocumentUno.alFrescoSessionTicket.replace("\"", ""));
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.RFC6532);
reqEntity.addPart("filedata", new FileBody(file));
reqEntity.addPart("alt_destination",new StringBody("workspace://SpacesStore/9a37fce5-2715-4730-a245-64f161304879"));
reqEntity.addPart("filename",new StringBody("ip_VM1985.txt"));
reqEntity.addPart("description", new StringBody("Descrizione del file"));
postMethod.setEntity(reqEntity);
System.out.println("executing request " + postMethod.getRequestLine());
CloseableHttpResponse resp = client.execute(postMethod);
Where AlfrescoRequests.getUploadRequest() gives the REST request URL (query-part excluded)
public static String getUploadRequest()
{
return DocumentUno.ECM_ADDRESS+"/alfresco/service/api/upload";
}
The upload is done correctly, i get a code 200, but the description is not set and i cannot specify a filename, that remains the same of the original file. Please tell me if you need other details.
The /alfresco/service/api/upload webscript unfortunately does not handle metadata. This is a known feature of Alfresco. The filename is handled but the description, which really is the cm:description property, like any other additional standard or custom properties that you might have, are not saved.
I believe the description parameter of the webscript is used as the first parameter for this API:
ScriptNode checkin(String history, boolean majorVersion)
... and has nothing to do with the document title/description.
There is an open-source plugin (which I authored) that overcomes this limitation by allowing to specify any additional metadata during the upload:
http://softwareloop.com/uploader-plus-an-alfresco-uploader-that-prompts-for-metadata/
It comprises a repo amp and a share amp. Since you're using web services, you just need the repo amp.