How to post json data using West Wind in VFP - visual-foxpro

There's a document from West Wind says you can post data using application/json format:
https://west-wind.com/webconnection/docs/_2110q21c9.htm
But when using it, actually it'll do a http get instead of post:
DO wwhttp
oHTTP=CREATEOBJECT("wwHttp")
oHTTP.nHTTPPostMode = 8
oHttp.cContentType = "application/json"
oHTTP.AddPostKey("name","somename")
lcURL = "https://mywebserver.com/"
lcXML = oHTTP.HTTPGet(lcURL)
If using nHTTPPostMode = 1 or 2, the http request parameters are not correctly formatted as json. If I change to 4, it's using Get instead of Post again. Is there anyway to get around this?

When you post JSON data you need to post a JSON document not key value pairs as you are doing in your example. Essentially you need to provide the full content - the entire JSON document as a string - that you're sending to the server (just like the example shows).
To post a JSON document looks like this:
DO wwHttp
oHTTP=CREATEOBJECT("wwHttp")
oHttp.cContentType = "application/json"
lcJson = "{ name: 'somename' }"
oHTTP.AddPostKey(lcJson) && this sets the entire post buffer
lcURL = "https://mywebserver.com/"
lcJson = oHTTP.HTTPGet(lcURL)
Using name value pairs only works with POST buffers for URLEncoded or Multipart documents - every other type of content is sent as raw data that you have to provide.
in version 6 and later you can also use the following simpler syntax to .Post data:
oHttp.cContentType = "application/json"
lcJson = oHttp.Post(lcUrl, lcJson)
If you want to create JSON documents under program control you can serialize data using the wwJsonSerializer class (also part of Client Tools). It can serialize from objects, arrays/collections and cursors.

Related

How to parse a json with dynamic property name in OIC?

I need to consume and parse incoming json from a third party system in my code. I used RestTemplate to do it. So the response from the system looks like below.
{ "data": { "05AAAFW9419M11Q": { "gstin": "05AAAFW9419M11Q", "error_cd": "SWEB_9035", "message": "Invalid GSTIN / UID" } } }
Now the problem is the property name ("05AAAFW9419M11Q" in this case) in dynamic and in the next response it would be another string. In this case, how can I parse this json as this is not fixed in Oracle Integration Cloud? Response wrapper is not capturing the data apart from the one that is used for configuring the adapter which is fair enough as fieldname itself is changing.
Is there is any workaround for this?
You will have to go to PL/SQL and dynamic SQL, and if it's always the value of gstin entry, you can get the path of the key with
select '$.data.' ||
json_query(js_column, '$.data.*.gstin') into v_key path from table_with_json_column where ... conditions... ;
(assuming there is only 1 "data" per JSON payload) to later build a dynamic query based on json_table.

Parse a JSON object using ASPjson

In Classic ASP (VBScript) I can do a general request of POST using request.form or GET using request.querystring which would give me the entire string that was sent.
But, I now need to receive a JSON object from a client side location.
This is an example of what it might look like:
{
"firstName": "John",
"lastName" : "Smith",
"age" : 25
}
How do I request this entire object (which I will then be parsing with ASP.JSON)?
PS: I know that I can probably convert the JSON object to a string on the client side, and then parse as text on the server side, but that feels like a work-around rather than a straight solution.
First of all, I wouldn't use that AspJson, but this: https://github.com/rcdmk/aspJSON
Second, you're not receiving an object per se, but request that contains a "string version of the json object". In this case, probably bytes, thats why you're going to BinaryRead and convert it into a body first.
You then will be able to parse the body with any parser you want.
Now let's try to give you an example code:
<%Response.LCID = 1033%>
<!--#include file="__jsonObject.class.v3.8.1.asp" -->
Set UTF8Enc = CreateObject("System.Text.UTF8Encoding") ' .NET COMPONENT, required on the server app pool
Set JSON = new JSONobject
lngBytesCount = Request.TotalBytes
request_body = UTF8Enc.GetString(Request.BinaryRead(lngBytesCount))
Set request_json = JSON.parse(request_body)
first_name = request_json("firstName")
last_name = request_json("lastName")
age = request_json("age")

How do I download all the abstract datas from the pubmed data ncbi

I want to download all the pubmed data abstracts.
Does anyone know how I can easily download all of the pubmed article abstracts?
I got the source of the data :
ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/af/12/
Is there anyway to download all these tar files..
Thanks in advance.
There is a package called rentrezhttps://ropensci.org/packages/. Check this out. You can retrieve abstracts by specific keywords or PMID etc. I hope it helps.
UPDATE: You can download all the abstracts by passing your list of IDS with the following code.
library(rentrez)
library(xml)
your.ids <- c("26386083","26273372","26066373","25837167","25466451","25013473")
# rentrez function to get the data from pubmed db
fetch.pubmed <- entrez_fetch(db = "pubmed", id = your.ids,
rettype = "xml", parsed = T)
# Extract the Abstracts for the respective IDS.
abstracts = xpathApply(fetch.pubmed, '//PubmedArticle//Article', function(x)
xmlValue(xmlChildren(x)$Abstract))
# Change the abstract names with the IDS.
names(abstracts) <- your.ids
abstracts
col.abstracts <- do.call(rbind.data.frame,abstracts)
dim(col.abstracts)
write.csv(col.abstracts, file = "test.csv")
I appreciate that this is a somewhat old question.
If you wish to get all the pubmed entries with python I wrote the following script a while ago:
import requests
import json
search_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&mindate=1800/01/01&maxdate=2016/12/31&usehistory=y&retmode=json"
search_r = requests.post(search_url)
search_data = search_r.json()
webenv = search_data["esearchresult"]['webenv']
total_records = int(search_data["esearchresult"]['count'])
fetch_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmax=9999&query_key=1&webenv="+webenv
for i in range(0, total_records, 10000):
this_fetch = fetch_url+"&retstart="+str(i)
print("Getting this URL: "+this_fetch)
fetch_r = requests.post(this_fetch)
f = open('pubmed_batch_'+str(i)+'_to_'+str(i+9999)+".json", 'w')
f.write(fetch_r.text)
f.close()
print("Number of records found :"+str(total_records))
It starts of by making an entrez/eutils search request between 2 dates which can be guaranteed to capture all of pubmed. Then from that response the 'webenv' (which saves the search history) and total_records are retrieved. Using the webenv capability saves having to hand the individual record ids to the efetch call.
Fetching records (efetch) can only be done in batches of 10000, the for loop handles grabbing batches of 9,999 records and saving them in labelled files until all the records are retrieved.
Note that requests can fail (non 200 http responses, errors), in a more robust solution you should wrap each requests.post() in a try/except. And before dumping/using the data to file you should ensure that the http response has a 200 status.

In Nest (Elasticsearch), how can I get the raw json mapping of an index?

I want to check the discrepancies between my current mapping (as in my C# code) and the mapping in the elasticsearch index.
With only:
var res = esClient.GetMapping<EsCompany>();
I get GetMappingResponse object in c#, I will have to compare field by field for equality. Even worse, each field has their own properties, I have to descend into those properties for further comparison.
In my application, I prefer obtaining the raw json of the mapping, and I can easily diff two json objects for equality.
I then tried this:
var res = esClient.Raw.IndicesGetMapping(myIndexName);
But when I read res.Response, I get an AmbiguousMatchException exception.
When you connect to Elasticsearch you can choose to expose the raw response like this:
var client = new ElasticClient(new ConnectionSettings().ExposeRawResponse());
Then you should be able to access the raw json via:
var json = res.ConnectionStatus.ResponseRaw;

How can I change the column name of an existing Class in the Parse.com Web Browser interface?

I couldn't find a way to change a column name, for a column I just created, either the browser interface or via an API call. It looks like all object-related API calls manipulate instances, not the class definition itself?
Anyone know if this is possible, without having to delete and re-create the column?
This is how I did it in python:
import json,httplib,urllib
connection = httplib.HTTPSConnection('api.parse.com', 443)
params = urllib.urlencode({"limit":1000})
connection.connect()
connection.request('GET', '/1/classes/Object?%s' % params, '', {
"X-Parse-Application-Id": "yourID",
"X-Parse-REST-API-Key": "yourKey"
})
result = json.loads(connection.getresponse().read())
objects = result['results']
for object in objects:
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
objectId = object['objectId']
objectData = object['data']
connection.request('PUT', ('/1/classes/Object/%s' % objectId), json.dumps({
"clonedData": objectData
}), {
"X-Parse-Application-Id": "yourID",
"X-Parse-REST-API-Key": "yourKEY",
"Content-Type": "application/json"
})
This is not optimized - you can batch 50 of the processes together at once, but since I'm just running it once I didn't do that. Also since there is a 1000 query limit from parse, you will need to do run the load multiple times with a skip parameter like
params = urllib.urlencode({"limit":1000, "skip":1000})
From this Parse forum answer : https://www.parse.com/questions/how-can-i-rename-a-column
Columns cannot be renamed. This is to avoid breaking an existing app.
If your app is still under development, you can just query for all the
objects in your class and copy the value of the old column to the new
column. The REST API is very useful for this. You may them drop the
old column in the Data Browser
Hope it helps
Yes, it's not a feature provided by Parse (yet). But there are some third party API management tools that you can use to rename the fields in the response. One free tool is called apibond.com
It's a work around, but I hope it helps

Resources