r API call for json data and converting to dataframe - json

I have tried to return a JSON response using an API call in RStudio. Its returning the desired JSON file in Postman but the httr API calls don't seem to be returning anything which I can parse into a dataframe.
This is the call in r using the httr package:
req <-
httr::POST("https://api2.elasticgrid.com/api/v1/analytics/vendor/partnerengagement/advanced/all",
httr::add_headers(
"Authorization" = "Bearer <long string>"
),
body = "VendorId=80&TimeFrame=AddMonths(-3)&Language=en-US",encode="json"
);
This returns a List of 10, but nothing useful and I am fairly sure the JSON content is not hiding in the list
I've tried:
js <- fromJSON(content(req,as="text"))
But, this is returning "An error has occurred"
or:
json <- httr::content(req, as = "parsed")
But this returns a non-discript key "7b 22 4d..."
This is the working API call in Postman (RAW HTTP version shown):
POST /api/v1/analytics/vendor/collateral/advanced/all HTTP/1.1
Host: api2.elasticgrid.com
Authorization: Bearer <long string>
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: <token>
{
"VendorId": 80,
"TimeFrame": "AddMonths(-3)",
"Language": "en-US"
}
Can anyone point me in the right direction to parse a JSON into a dataframe based on an r API call. I've tried a few packages and resources already but am really not sure what to try next assuming this is possible.

The pblm might be coming from the API :
req <-
httr::POST("https://api2.elasticgrid.com/api/v1/analytics/vendor/partnerengagement/advanced/all",
httr::add_headers(
"Authorization" = "Bearer <long string>"
),
body = "VendorId=80&TimeFrame=AddMonths(-3)&Language=en-US",encode="json"
)
req$status_code
[1]500
The status code here indicates that the server is not responding well. You need to have a 200 here, which is the code for success. Check the Wikipedia page for more info: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Is the call you provided the exact call you're making? If this is the case, you might want to check your call again. The API might need an access token to grant the access.
For what concerns your situation, the "7b 22 4d..." is a raw string. you can convert it with rawToChar.
Here's a simple workflow with a call on the haveIbeenpwned API.
#GET the url
req <- httr::GET("https://haveibeenpwned.com/api/v2/breachedaccount/test#example.com")
req$status_code
[1] 200
# extract req$content
cont <- req$content
cont
[1] 5b 7b 22 54 69 74 6c 65 22 3a 22 30 30 30 77 65 62 68 6f 73 74 22 2c 22 4e 61 6d 65
[29] 22 3a 22 30 30 30 77 65 62 68 6f 73 74 22 2c 22 44 6f 6d 61 69 6e 22 3a 22 30 30 30
[57] 77 65 62 68 6f 73 74 2e 63 6f 6d 22 2c 22 42 72 65 61 63 68 44 61 74 65 22 3a 22 32
[85] 30 31 35 2d 30 33 2d 30 31 22 2c 22 41 64 64 65 64 44 61 74 65 22 3a 22 32 30 31 35
[113] 2d 31 30 2d 32 36 54 32 33 3a 33 35 3a 34 35 5a 22 2c 22 4d 6f 64 69 66 69 65 64 44
[141] 61 74 65 22 3a 22 32 30 31 35 2d 31 30 2d 32 36 54 32 33 3a 33 35 3a 34 35 5a 22 2c
#Convert to char
char <- rawToChar(req$content)
char
[1] "[{\"Title\":\"000webhost\",\"Name\":
\"000webhost\",\"Domain\":\"000webhost.com\",
\"BreachDate\":\"2015-03-01\",\"AddedDate\":\""
...
#Convert to df
df <- jsonlite::fromJSON(char)
df
Title Name Domain BreachDate
1 000webhost 000webhost 000webhost.com 2015-03-01
2 Adobe Adobe adobe.com 2013-10-04
3 Bitcoin Talk BitcoinTalk bitcointalk.org 2015-05-22
4 BTC-E BTCE btc-e.com 2014-10-01
5 Dailymotion Dailymotion dailymotion.com 2016-10-20
6 Dropbox Dropbox dropbox.com 2012-07-01
...

Related

LOAD DATA INFILE consistently skips first line, while not set to IGNORE

I'm trying to load a csv file with stock prices into a prices table.
The csv file has 3 lines that I want to ignore, including a blank one.
When I set IGNORE 1 LINES, it runs into an error processing the column headers.
When I set IGNORE 2 LINES, it consistently skips the first line of data.
All other data is loaded just fine, starting from the 2nd data row ("11-03-2020" in this case).
How do I fix this, without changing the data in the csv?
The csv looks like this:
"Some instructions"
"date";"price"
"12-03-2020";133.08
"11-03-2020";143.68
"10-03-2020";149.14
...
The CREATE TABLE code:
CREATE TABLE `prices` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`isin` CHAR(12) NOT NULL,
`price_date` DATE NOT NULL,
`price` DECIMAL(10,2) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `isin_date` (`isin`, `price_date`),
CONSTRAINT `prices_stocks` FOREIGN KEY (`isin`) REFERENCES `stocks` (`isin`) ON UPDATE CASCADE ON DELETE NO ACTION
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
ROW_FORMAT=DYNAMIC
AUTO_INCREMENT=157532
;
The LOAD DATA SQL statement:
LOAD DATA LOCAL INFILE 'price_history_LU0792910050.csv'
REPLACE INTO TABLE stock_db.prices
CHARACTER SET utf8
FIELDS TERMINATED BY ';'
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 2 LINES
(#vdate, #vprice)
SET
isin = 'LU0792910050',
price_date = STR_TO_DATE(#vdate, '%d-%m-%Y'),
price = #vprice;
hexdump of an example csv:
00000000: EF BB BF 22 44 65 20 69 6E 73 74 65 6C 6C 69 6E ..."De instellin
00000010: 67 65 6E 20 76 61 6E 20 75 77 20 45 78 63 65 6C gen van uw Excel
00000020: 2D 73 6F 66 74 77 61 72 65 20 6B 75 6E 6E 65 6E -software kunnen
00000030: 20 64 65 20 77 65 65 72 67 61 76 65 20 76 61 6E de weergave van
00000040: 20 6F 6E 64 65 72 73 74 61 61 6E 64 65 20 67 65 onderstaande ge
00000050: 67 65 76 65 6E 73 20 62 65 C3 AF 6E 76 6C 6F 65 gevens be..nvloe
00000060: 64 65 6E 20 64 6F 6F 72 20 64 65 20 63 65 6C 6F den door de celo
00000070: 70 6D 61 61 6B 20 76 61 6E 20 64 65 20 67 65 67 pmaak van de geg
00000080: 65 76 65 6E 73 63 61 74 65 67 6F 72 69 65 20 28 evenscategorie (
00000090: 62 69 6A 76 2E 20 61 61 6E 74 61 6C 20 69 6E 20 bijv. aantal in
000000A0: 70 6C 61 61 74 73 20 76 61 6E 20 64 61 74 75 6D plaats van datum
000000B0: 29 2E 22 0D 0A 0D 0A 22 64 61 74 65 22 3B 22 70 )."...."date";"p
000000C0: 72 69 63 65 22 0D 0A 22 31 35 2D 30 37 2D 32 30 rice".."15-07-20
000000D0: 32 30 22 3B 35 31 2E 37 36 0D 0A 22 31 34 2D 30 20";51.76.."14-0
000000E0: 37 2D 32 30 32 30 22 3B 35 31 2E 31 37 0D 0A 22 7-2020";51.17.."
000000F0: 31 33 2D 30 37 2D 32 30 32 30 22 3B 35 31 2E 30 13-07-2020";51.0
00000100: 33 0D 0A 22 31 30 2D 30 37 2D 32 30 32 30 22 3B 3.."10-07-2020";
(Hexdump was not installed on Synology, so used Python hexdump. Hope this works)
"12-03-2020" cannot be put directly be put into a DATE column. Instead, put it into an # variable, then use str_to_date(...). (Let us know if you need help; there are many examples floating around.)
I see C3 AF, which is utf8 for ï, as in ... beïnvloegen ... -- Does that sound "right"? CHARACTER SET utf8 should have read it correctly.
The initial EF BB BF is "BOM". I don't know if LOAD FILE is smart enough to silently skip over it. This may be causing your problem. One approach is to edit the file to remove the first 3 bytes.
Later comes 0D 0A 0D 0A, which matches your description of there being 3 header lines, the second being blank. And LINES TERMINATED BY '\r\n' should be correct for that.

Convert captured packet data to html in C

I have captured a packet using libpcap and have the packet data in a char array. When I print it in hex and ascii the output is
47 45 54 20 2f 62 72 6f 77 73 65 2f 64 69 63 74 GET /browse/dict
69 6f 6e 61 72 79 3f 73 3d 74 20 48 54 54 50 2f ionary?s=t HTTP/
31 2e 31 0d 0a 48 6f 73 74 3a 20 77 77 77 2e 64 1.1..Host: www.d
69 63 74 69 6f 6e 61 72 79 2e 63 6f 6d 0d 0a 55 ictionary.com..U
73 65 72 2d 41 67 65 6e 74 3a 20 4d 6f 7a 69 6c ser-Agent: Mozil
.....
......
41 31 25 32 43 31 30 31 25 33 41 31 25 32 43 32 A1%2C101%3A1%2C2
25 33 41 31 25 32 43 31 30 32 25 33 41 31 25 32 %3A1%2C102%3A1%2
43 31 30 33 25 33 41 31 25 32 43 33 25 33 41 31 C103%3A1%2C3%3A1
............
.....
Now I want to convert above http request/response from captured packet to html tags. How can I do that?

How to filter regex in SQL developer

I have receive some kind of below regrex message with random set as below pattern. I want to filter this message by insert filter table in SQL developer how to make the pattern to filter out. Query to insert the filter is below.
37 35 32 41 44 44 37 36 32 46 34 34 34 45 45 34 42 31 31 34 31 41 36 43 39
44 37 41 35 45 35 39 20 44 61 74 65 54 69 6D 65 20 3A 20 53 75 6E 20 41 75 67 20 32 30
20 32 30 3A 30 37 3A 33 39 20 41 53 54 20 32 30 31 37 2C 20 65 72 72 6F 72 4D 65 73 73
61 67 65 20 3A 20 45 78 63 65 70 74 69 6F 6E 20 67 65 6E 65 72 61 74 65 64 20 6F 6E 20
D9 A2 D9 A0 D9 A1 D9 A7 2D D9 A0 D9 A8 2D D9 A2 D9 A0 20 D9 A2 D9 A0 3A D9 A0 D9 A7 3A D9
A5 D9 A0 0A 0A 45 78 63 65 70 74 69 6F 6E 3A 20 6A 61 76 61 2E 6C
insert
into FILTER_IGNR (
ID_ALARM_FILTER_IGNR,
FILTER_PATTERN,
CREATED_DATE_TIME,
UPDATED_DATE_TIME,
IS_ACTIVE)
select MAX(ID_ALARM_FILTER_IGNR)+1,
'.*ERROR.*', -- Check and change in this one
sysdate,
sysdate,
1
from FILTER_IGNR

Download Firefox browser cache file

I was working on a project and just when I saved the file, my power cut off. Now my html file is corrupted and I have no backup of it.
I just remembered that I might have a cache of it in Firefox. So, first I tried CacheViewer from Firefox official add-ons store. This add-on would install and then required a restart, after the restart it would disappear.
So instead, I tried the old fashioned way. I went to about:cache > Local Storage and whew, I was lucky enough that it was there and wasn't replaced with the blank corrupted html file.
Now I have some sort (Catch Entry Information) of file. The left side has the binary code, while the right side has the proper html code with some unnecessary . periods in it.
This is what my this file looks like:
00000000: 3c 21 64 6f 63 74 79 70 65 20 68 74 6d 6c 3e 0d <!doctype html>.
00000010: 0a 3c 68 74 6d 6c 3e 0d 0a 3c 68 65 61 64 3e 0d .<html>..<head>.
00000020: 0a 3c 6d 65 74 61 20 63 68 61 72 73 65 74 3d 22 .<meta charset="
00000030: 75 74 66 2d 38 22 3e 0d 0a 3c 74 69 74 6c 65 3e utf-8">..<title>
00000040: 46 69 74 41 70 70 3c 2f 74 69 74 6c 65 3e 0d 0a FitApp</title>..
00000050: 3c 73 74 79 6c 65 3e 40 66 6f 6e 74 2d 66 61 63 <style>#font-fac
00000060: 65 20 7b 0a 20 20 66 6f 6e 74 2d 66 61 6d 69 6c e {. font-famil
00000070: 79 3a 20 22 52 6f 62 6f 74 6f 2d 6c 69 67 68 74 y: "Roboto-light
00000080: 22 3b 0a 20 20 73 72 63 3a 20 75 72 6c 28 22 2e ";. src: url(".
00000090: 2e 2f 66 6f 6e 74 73 2f 52 6f 62 6f 74 6f 2d 4c ./fonts/Roboto-L
000000a0: 69 67 68 74 2e 74 66 66 22 29 20 66 6f 72 6d 61 ight.tff") forma
000000b0: 74 28 22 74 72 75 65 74 79 70 65 22 29 2c 20 6c t("truetype"), l
000000c0: 6f 63 61 6c 28 22 52 6f 62 6f 74 6f 2d 6c 69 67 ocal("Roboto-lig
000000d0: 68 74 22 29 20 66 6f 72 6d 61 74 28 22 74 72 75 ht") format("tru
000000e0: 65 74 79 70 65 22 29 3b 0a 20 20 66 6f 6e 74 2d etype");. font-
000000f0: 77 65 69 67 68 74 3a 20 34 30 30 3b 0a 7d 0a 40 weight: 400;.}.#
How do I go about decoding this?
If there is a better way to download a catch file from Mozilla Firefox, please do mention.
Steps:
Copy all of that text into a text document, and save it.
Edit that text to make it only the column of hexadecimal characters.
Options for how to do this include shell commands, a find-replace using regex, or a column editing mode in your text editor.
Convert those hex encoded bytes into a binary file.
You should be able to paste those characters into any decent hex editor, and save it as a file.
BTW, the .'s in this case are substituted to replace the newline characters.

How can I send an HTTP request to my router with a not well-formatted field?

I'm trying to find a way to enable/disable my router whithout the need for manually using the browser. After a bit of snooping, it seems to me that I have to send a request like that [from WireShark]:
0000 9c 97 26 c9 c2 20 b0 48 7a 80 52 9c 08 00 45 00 ..&.. .Hz.R...E.
0010 02 2e 28 89 40 00 80 06 00 00 c0 a8 00 02 c0 a8 ..(.#...........
0020 00 01 d6 a8 00 50 34 fd c9 8d c9 ef dd 2d 50 18 .....P4......-P.
0030 fe da 83 74 00 00 50 4f 53 54 20 2f 77 61 6e 53 ...t..POST /wanS
0040 74 61 74 75 73 2e 6c 70 20 48 54 54 50 2f 31 2e tatus.lp HTTP/1.
0050 31 0d 0a 48 6f 73 74 3a 20 31 39 32 2e 31 36 38 1..Host: 192.168
0060 2e 30 2e 31 0d 0a 55 73 65 72 2d 41 67 65 6e 74 .0.1..User-Agent
0070 3a 20 4d 6f 7a 69 6c 6c 61 2f 35 2e 30 20 28 57 : Mozilla/5.0 (W
0080 69 6e 64 6f 77 73 20 4e 54 20 36 2e 31 3b 20 57 indows NT 6.1; W
0090 4f 57 36 34 3b 20 72 76 3a 33 36 2e 30 29 20 47 OW64; rv:36.0) G
00a0 65 63 6b 6f 2f 32 30 31 30 30 31 30 31 20 46 69 ecko/20100101 Fi
00b0 72 65 66 6f 78 2f 33 36 2e 30 0d 0a 41 63 63 65 refox/36.0..Acce
00c0 70 74 3a 20 74 65 78 74 2f 68 74 6d 6c 2c 61 70 pt: text/html,ap
00d0 70 6c 69 63 61 74 69 6f 6e 2f 78 68 74 6d 6c 2b plication/xhtml+
00e0 78 6d 6c 2c 61 70 70 6c 69 63 61 74 69 6f 6e 2f xml,application/
00f0 78 6d 6c 3b 71 3d 30 2e 39 2c 2a 2f 2a 3b 71 3d xml;q=0.9,*/*;q=
0100 30 2e 38 0d 0a 41 63 63 65 70 74 2d 4c 61 6e 67 0.8..Accept-Lang
0110 75 61 67 65 3a 20 69 74 2d 49 54 2c 69 74 3b 71 uage: it-IT,it;q
0120 3d 30 2e 38 2c 65 6e 2d 55 53 3b 71 3d 30 2e 35 =0.8,en-US;q=0.5
0130 2c 65 6e 3b 71 3d 30 2e 33 0d 0a 41 63 63 65 70 ,en;q=0.3..Accep
0140 74 2d 45 6e 63 6f 64 69 6e 67 3a 20 67 7a 69 70 t-Encoding: gzip
0150 2c 20 64 65 66 6c 61 74 65 0d 0a 44 4e 54 3a 20 , deflate..DNT:
0160 31 0d 0a 52 65 66 65 72 65 72 3a 20 68 74 74 70 1..Referer: http
0170 3a 2f 2f 31 39 32 2e 31 36 38 2e 30 2e 31 2f 77 ://192.168.0.1/w
0180 61 6e 53 74 61 74 75 73 2e 6c 70 0d 0a 43 6f 6f anStatus.lp..Coo
0190 6b 69 65 3a 20 78 41 75 74 68 5f 53 45 53 53 49 kie: xAuth_SESSI
01a0 4f 4e 5f 49 44 3d 49 4d 45 74 4f 38 44 4c 45 73 ON_ID=IMEtO8DLEs
01b0 51 70 6d 54 77 69 64 67 43 59 6c 51 41 3d 0d 0a QpmTwidgCYlQA=..
01c0 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 6b 65 65 70 Connection: keep
01d0 2d 61 6c 69 76 65 0d 0a 43 6f 6e 74 65 6e 74 2d -alive..Content-
01e0 54 79 70 65 3a 20 61 70 70 6c 69 63 61 74 69 6f Type: applicatio
01f0 6e 2f 78 2d 77 77 77 2d 66 6f 72 6d 2d 75 72 6c n/x-www-form-url
0200 65 6e 63 6f 64 65 64 0d 0a 43 6f 6e 74 65 6e 74 encoded..Content
0210 2d 4c 65 6e 67 74 68 3a 20 32 39 0d 0a 0d 0a 72 -Length: 29....r
0220 6e 3d 49 4d 45 74 4f 38 44 4c 45 73 51 70 6d 54 n=IMEtO8DLEsQpmT
0230 77 69 64 67 43 59 6c 51 41 25 33 44 widgCYlQA%3D
As you can see, it ends with 'rn=IMEtO8DLEsQpmTwidgCYlQA%3D', that is the session cookie.
Putting a 'normal' request the router goes back to index instead of performing the action, probably for the lack of 'rn=IMEtO8DLEsQpmTwidgCYlQA%3D'.
Until now, I tried using Wget (under windows...) but I can't find a way to add the last info.
So I would know if
there is a way to add an header that is not header-compliant (use of = instead of : ) using wGet or if
there is a tool more suited for that task (I'm looking for cUrl but I don't think it acts different from wGet).
The batch file (plain ol' DOS) I wrote for testing wGet is:
#title %~n0
#cls
#set _wget="C:\Program Files (x86)\GnuWin32\bin\wget"
#set _url=http://192.168.0.1/
#set _urls=http://192.168.0.1/standard.lp
#set _urlw=http://192.168.0.1/wanStatus.lp
#set _post=--post-data="/wanStatus.lp HTTP/1.1"
#set _cook=./cookies.txt
#set _hdr1="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
#set _hdr2="Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3"
#set _hdr3="Accept-Encoding: gzip, deflate"
#set _hdr4="DNT: 1"
#set _agnt="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0"
#set _rfr="Referer: http://192.168.0.1/wanStatus.lp"
#set _opt=--server-response --timestamping --user-agent=%_agnt% --keep-session-cookies --referer=%_rfr% --save-headers --header=%_hdr1% --header=%_hdr2% --header=%_hdr3% --header=%_hdr4% --debug
%_wget% %_opt% --save-cookies %_cook% %_url%
%_wget% %_opt% --load-cookies %_cook% %_urlw%
#FOR /F "eol=# tokens=1,6,7* delims= " %%i in (F:\wget\cookies.txt) do #(
#echo i=%%i j=%%j k=%%k l=%%l
#set _ckhost=%%i
#set _ckname=%%j
#set _ckval=%%k
)
#echo host=%_ckhost% cookie name=%_ckname% value=%_ckval%
#if "%_ckname%"=="xAuth_SESSION_ID" set _rn=rn=%_ckval%
#if "%_rn:~-1%"=="=" set _rn=%_rn:~0,-1%%%3D
#echo _rn='%_rn%'
%_wget% %_opt% --load-cookies %_cook% %_post% --header="%_rn%" %_url%
but it ends up this way:
F:\wget>"C:\Program Files (x86)\GnuWin32\bin\wget" --server-response --timestamping --user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0" --keep-session-cookies --referer="Referer: http://192.168.0.1/wanStatus.lp" --save-headers --header="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" --header="Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3" --header="Accept-Encoding: gzip, deflate" --header="DNT: 1" --debug --load-cookies ./cookies.txt --post-data="/wanStatus.lp HTTP/1.1" --header="rn=MGyN4YpepJoE6kSXPVT9lgA%3D" http://192.168.0.1/
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\Program Files (x86)\GnuWin32/etc/wgetrc
Setting --load-cookies (loadcookies) to ./cookies.txt
Setting --post-data (postdata) to /wanStatus.lp HTTP/1.1
Setting --header (header) to rn=MGyN4YpepJoE6kSXPVT9lgA%3D
C:\Program Files (x86)\GnuWin32\bin\wget: --header: intestazione "rn=MGyN4YpepJoE6kSXPVT9lgA%3D" non valida.
Thank you very much for any suggestion! Or for links pointing to pages where I can find a solution (I spent a looot of time googling around...).
Luca
Note: I think it's useless to add here the HTML and JS code of the page in the router that generate the line rn=MGyN4YpepJoE6kSXPVT9lgA%3D, but if you need it, I can report it.
Look careful at the captured data and you might notice that these data are not part of the header. The header ends with \r\n\r\n (Bytes 021b..021e) and your "session cookie" is not a cookie (which would be part of the header) but the body of the POST request. Thus you need to use --post-data 'rn=MGyN...'