I have an html output in the form of a table and I want to send that with my JSON eventually via a POST for an api.
I have removed tags and troubleshot it but doesnt work. Is there a specific library that I can use in R to achieve this parsing?
I am using simple paste() to parse that html page and insert it into JSON's 'value'.
I first save my data.frame(list) as html then I try to parse it.
mydoc = bsdoc( title = 'xxx' )
mydoc= addFlexTable( mydoc, flextable = output_as_flex)
mydoc = addParagraph( mydoc, value = "<br>")
writeDoc(mydoc,file ="C://yyy.html")
tree=htmlTreeParse("C://yyy.html")
Here I follow the Confluence Rest Api's format to form this string for JSON. Data has to be passed(I am passing a variable in paste) as html where 'h_h'
data <- paste(sep='','{"type":"page","title":',
'"xxx",',
'"ancestors":[{"id":',123123,'}],"space":{"key":"','xxx','"},"body":{"storage":{"value":"',h_h,'","representation":"storage"}}}')
httr::set_config(config(ssl_verifypeer=FALSE))
URL <- "xxx/rest/api/content"
response = POST(URL, authenticate("xx", "xx"), body = data, verbose(),add_headers('Content-Type' = 'application/json;charset=utf-8','Cache-Control'='no-cache'))
response
http_status(response)
warn_for_status(response)
stop_for_status(response)
I want to upload that html table formed as output_as_flex to the api and eventually to the confluence page.
Thanks.
Related
I'm getting the response in html. All tutorials are using:
var y = document.getElementsByClassName('mw-search-result-heading');
or
var jsonData = JSON.parse(response);
and they both fail.
First one due to document not defined, second because response is not JSON but HTML.
So, how can I correctly parse an html in Postman ?
Hi you can use cheerio jQuery api
Example:
const $ = cheerio.load(pm.response.text();
console.log($("title").text()); // get title
In Postman I have did in the below way and want to do the same in Rest Assured framework. I want to parse and save the "tltkt" value in Rest Assured framework. How can I do that in Rest Assured?
GET call :https://prod.streaming/com/account/signin/
Postman tests:**
Load the HTML response to $
const $ = cheerio.load(pm.response.text())
console.log($("title").text()); // get title
console.log($('script#app-config').text().trim());
let appConfig = JSON.parse($('script#app-config').text().trim());
console.log(appConfig.tltkt);
pm.collectionVariables.set("saved_tl_tkt", appConfig.tl_tkt);
console.log(pm.collectionVariables.get("saved_tl_tkt"), ":from pm");
Response in HTML:
main id="main-container"
script id="app-config" type="application/json"
{"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""};
I assume that you've already had {"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""}
Because it's not valid json, so I use regex to extract value from this string.
String bodyTxt = ...;
Pattern pattern = Pattern.compile("\"tltkt\":\"(.+?)\"");
Matcher matches = pattern.matcher(bodyTxt);
String tltkt = "";
if(matches.find()) {
tltkt = matches.group(1);
}
System.out.println(tltkt);
//QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ
I have a function in my views.py which returns JSON data, what I have in mind is rendering a HTML page from that data.
The same question can also be associated with data returned from rest API generic views returning JSON data.
I didn't understand your concern, but you can send the json data to your template using json.dumps()
response_data = {}
response_data['name'] = 'test'
response_data['message'] = 'Hi test'
render(request, "test.html", {'json_data': json.dumps(response_data)})
Now, you can access json_data in your template
I want to receive a json string as a response from a REST API URL.
It has
header as Content-Type=application/json.
It should have a body with json format
eg-{"string1":"string2","string3":"string4"}
These are the details I am inputting when using POSTMAN. What is the correct Syntax for the above requirement.
I am trying the following syntax but it always throws an error:
POST(url = login,add_headers('Content-Type'='application/json'),body = c("string1"="string2","string3"="string4"),encode = c("json"),verbose())
Try using this
POST(url = login,add_headers('Content-Type'='application/json'),body = list(string1="string2",string3="string4"),encode = "json",verbose())
I am trying to read json response from this link. But its not working! I get the following error:
ValueError: No JSON object could be decoded.
Here is the code I've tried:
import urllib2, json
a = urllib2.urlopen('https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA&screenshot=true&snapshots=true&locale=en_US&url=https://www.economicalinsurance.com/en/&strategy=mobile&filter_third_party_resources=false&callback=_callbacks_._DElanZU7Xh1K')
data = json.loads(a)
I made these changes:
import requests, json
r=requests.get('https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA&screenshot=true&snapshots=true&locale=en_US&url=https://www.economicalinsurance.com/en/&strategy=mobile&filter_third_party_resources=false')
json_data = json.loads(r.text)
print json_data['ruleGroups']['USABILITY']['score']
A Quick question - Construct Image link .
I able to get here : -
from selenium import webdriver
txt = json_data['screenshot']['data']
txt = str(txt).replace('-','/').replace('_','/')
#then in order to construct the image link i tried : -
image_link = 'data:image/jpeg;base64,'+txt
driver = webdriver.Firefox()
driver.get(image_link)
The problem is i am not getting the image, also the len(object_original) as compared len(image_link) differs . Could anybody please advise the right elements missing in my constructed image link ?. Thank you
Here is API link - https://www.google.co.uk/webmasters/tools/mobile-friendly/ Sorry added it late .
Two corrections need to be made to your code:
The url was corrected (as mentioned by Felix Kling here). You have to remove the callback parameter from the GET request you were sending.
Also, if you check the type of the response that you were fetching earlier you'll notice that it wasn't a string. It was <type 'instance'>. And since json.loads() accepts a string as a parameter variable you would've got another error. Therefore, use a.read() to fetch the response data in string.
Hence, this should be your code:
import urllib2, json
a = urllib2.urlopen('https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA&screenshot=true&snapshots=true&locale=en_US&url=https://www.economicalinsurance.com/en/&strategy=mobile&filter_third_party_resources=false')
data = json.loads(a.read())
Answer to your second query (regarding the image) is:
from base64 import decodestring
arr = json_data['screenshot']['data']
arr = arr.replace("_", "/")
arr = arr.replace("-","+")
fh = open("imageToSave.jpeg", "wb")
fh.write(str(arr).decode('base64'))
fh.close()
Here, is the image you were trying to fetch - Link
Felix Kling is right about the address, but I also created a variable that holds the URL. You can try this out to and it should work:
import urllib2, json
url = "https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA&screenshot=true&snapshots=true&locale=en_US&url=https://www.economicalinsurance.com/en/&strategy=mobile&filter_third_party_resources=false"
response = urllib2.urlopen(url)
data = json.loads(response.read())
print data