Is it possible to modify a variable in Postman - javascript

I'm using Postman and I am trying to chain together some requests. There is an identity string that is generated in my first request that I would like to use in my second request e.g. similar to searching for a product, and then adding that product to basket.
Now I've been able to pull the value from the first request and I can pick that up in the second request. The problem is that the identity string has an ampersand in it. When I post the second request, it throws an error because the ampersand has not been escaped in the string. I would like to replace the ampersand in the variable with "&" but I can't get this to work.
I'm new to JavaScript so I imagine this is where the problem is. In Postman I have:
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);
postman.setEnvironmentVariable("ItineraryId", jsonObject.ItineraryId);
ItineraryId.replace("&","&");
This returns "There was an error in evaluating the test script: ItineraryId is not defined". So I tried:
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);
var oldId = postman.setEnvironmentVariable("ItineraryId", jsonObject.ItineraryId);
oldId.replace("&","&");
And got "There was an error in evaluating the test script: Cannot read property 'replace' of undefined"
Thanks in advance.

Figured it out!
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);
var itineraryId = jsonObject.ItineraryId;
itineraryId = itineraryId.replace("&","&");
postman.setEnvironmentVariable("ItineraryId", itineraryId);

You should user pm.collectionVariables.set like in example below :
pm.collectionVariables.set("userId", pm.response.headers.get('Location').replace(pm.environment.get("api_url") + "/api/users/", ""));
In this example after POST on /api/users in response header : Location we receive link to new resource. You can use other variables like : pm.environment.get("api_url")

Related

Parse JSON data from a website with HTTP request with javascript

I've used package sniffer to get a specific endpoint, which gave me a direct JSON url. Now I want to get for all usernames from this JSON format.For eg. it goes like this:
{"node":{"id":"1428809588","username":"kafeterijabg","full_name":"\u041a\u0430\u0444\u0435\u0442\u0435\u0440\u0438\u0458\u0430\u00ae","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/65f96409e2a6c5b9b72cda956aa0dfea/5E638B5F/t51.2885-19/s150x150/64620072_350924665805634_2914908438809018368_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"1428809588","expiring_at":1572193230,"has_pride_media":false,"latest_reel_media":1572095740,"seen":1572031349,"owner":{"__typename":"GraphUser","id":"1428809588","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/65f96409e2a6c5b9b72cda956aa0dfea/5E638B5F/t51.2885-19/s150x150/64620072_350924665805634_2914908438809018368_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"kafeterijabg"}}}}
I've tried sending requests and parsing it to console but I'm getting "undefined".
function Get(yourUrl){
var Httpreq = new XMLHttpRequest();
Httpreq.open("GET",yourUrl,false);
Httpreq.send(null);
return Httpreq.responseText;
}
var json_obj = JSON.parse(Get(MYURL));
console.log(json_obj.username);
I've never done anything similar before.
EDIT1:
It seems like I was using the wrong endpoint.But when I just try to get the output of the request without parsing it I'm getting only first two lines of It, even tho when I open the endpoint in browser I'm seeing all of it.
This is what I'm getting:
{"data":{"user":{"edge_follow":{"count":377,"page_info":{"has_next_page":false,"end_cursor":null},"edges":[]}}},"status":"ok"}
And here is what I expected to get.
{"data":{"user":{"edge_follow":{"count":377,"page_info":{"has_next_page":true,"end_cursor":"QVFBd2J1MzJKakRoNXI3bzVqR1hTYkRYRjJ0TGJrNjJxeFVnRmVsc0JwczZLR1d3VXloZDA4NXFiUFV6dXU4RlRfck93M0J5Z1ZINVdieEtwREVmWFZNaA=="},"edges":[{"node":{"id":"8752655712","username":"dragibravo","full_name":"Dragi Bravo \u00a9","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/5f53d36c1e430cbd5453478159c0a780/5E4CE6DD/t51.2885-19/s150x150/44656034_343135116439319_1392780490192191488_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"8752655712","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572123916,"seen":1572085057,"owner":{"__typename":"GraphUser","id":"8752655712","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/5f53d36c1e430cbd5453478159c0a780/5E4CE6DD/t51.2885-19/s150x150/44656034_343135116439319_1392780490192191488_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"dragibravo"}}}},{"node":{"id":"21396562691","username":"dete_devedesetih","full_name":"detedevedesetih","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/88d2c2221aaa75ca2ea16ab32d0f1578/5E42CEE5/t51.2885-19/s150x150/69694071_2580132948676320_8436138521869156352_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"21396562691","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572123552,"seen":null,"owner":{"__typename":"GraphUser","id":"21396562691","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/88d2c2221aaa75ca2ea16ab32d0f1578/5E42CEE5/t51.2885-19/s150x150/69694071_2580132948676320_8436138521869156352_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"dete_devedesetih"}}}},{"node":{"id":"14659853","username":"brittanya187","full_name":"Brittanya Razavi","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/f611badd19896aad1280c5743b4692d5/5E5907C2/t51.2885-19/s150x150/42002550_2204067873162229_8645854606632419328_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":true,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"14659853","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572121817,"seen":1572039263,"owner":{"__typename":"GraphUser","id":"14659853","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/f611badd19896aad1280c5743b4692d5/5E5907C2/t51.2885-19/s150x150/42002550_2204067873162229_8645854606632419328_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"brittanya187"}}}},{"node":{"id":"1465895116","username":"britnibrid","full_name":"neispavana lepotica","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ce45b2702afa309ca3c581dbd99ba68f/5E5A1E14/t51.2885-19/s150x150/44828900_327180521439297_1922646860288229376_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"1465895116","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"1465895116","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ce45b2702afa309ca3c581dbd99ba68f/5E5A1E14/t51.2885-19/s150x150/44828900_327180521439297_1922646860288229376_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"britnibrid"}}}},{"node":{"id":"1428809588","username":"kafeterijabg","full_name":"\u041a\u0430\u0444\u0435\u0442\u0435\u0440\u0438\u0458\u0430\u00ae","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/65f96409e2a6c5b9b72cda956aa0dfea/5E638B5F/t51.2885-19/s150x150/64620072_350924665805634_2914908438809018368_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"1428809588","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572107760,"seen":1572107760,"owner":{"__typename":"GraphUser","id":"1428809588","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/65f96409e2a6c5b9b72cda956aa0dfea/5E638B5F/t51.2885-19/s150x150/64620072_350924665805634_2914908438809018368_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"kafeterijabg"}}}},{"node":{"id":"13795435410","username":"jacobs_rs","full_name":"JacobsSrbija","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/4363dbdcf8e8a94bc65968dd4907ff60/5E64F5DB/t51.2885-19/s150x150/60911497_2333358360317600_4108961307611365376_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"13795435410","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572081948,"seen":1572081948,"owner":{"__typename":"GraphUser","id":"13795435410","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/4363dbdcf8e8a94bc65968dd4907ff60/5E64F5DB/t51.2885-19/s150x150/60911497_2333358360317600_4108961307611365376_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"jacobs_rs"}}}},{"node":{"id":"6706638255","username":"apartment_a6_kopaonik","full_name":"Apartment A6 Kopaonik \u00ae","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/db9d49897114fb812add6b89f60198da/5E54AB81/t51.2885-19/s150x150/45378715_351931902032023_3560095462486704128_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"6706638255","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":1572023751,"owner":{"__typename":"GraphUser","id":"6706638255","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/db9d49897114fb812add6b89f60198da/5E54AB81/t51.2885-19/s150x150/45378715_351931902032023_3560095462486704128_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"apartment_a6_kopaonik"}}}},{"node":{"id":"21433171875","username":"dugestaze_svilajnac","full_name":"DUGE STAZE","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/d5e220b083bfb4432cd6f4dda3a81ba7/5E47E625/t51.2885-19/s150x150/71523182_308405693359685_2542825728215351296_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"21433171875","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"21433171875","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/d5e220b083bfb4432cd6f4dda3a81ba7/5E47E625/t51.2885-19/s150x150/71523182_308405693359685_2542825728215351296_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"dugestaze_svilajnac"}}}},{"node":{"id":"280846376","username":"dekstroza","full_name":"dekstroza","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/838e15717ddeba3ff12d82657f3fa30e/5E5F3CCB/t51.2885-19/s150x150/70473686_396716634369751_3090030692306780160_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"280846376","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572101572,"seen":1572101572,"owner":{"__typename":"GraphUser","id":"280846376","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/838e15717ddeba3ff12d82657f3fa30e/5E5F3CCB/t51.2885-19/s150x150/70473686_396716634369751_3090030692306780160_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"dekstroza"}}}},{"node":{"id":"6619361585","username":"ozbiljne_teme","full_name":"Ozbiljne Teme","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/e56294e9abfe45f7c2ae2de54d208557/5E50CBDA/t51.2885-19/s150x150/53740023_372495643602512_1074420510437146624_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"6619361585","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572099187,"seen":1572099187,"owner":{"__typename":"GraphUser","id":"6619361585","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/e56294e9abfe45f7c2ae2de54d208557/5E50CBDA/t51.2885-19/s150x150/53740023_372495643602512_1074420510437146624_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"ozbiljne_teme"}}}},{"node":{"id":"706629897","username":"mylifeaskitty","full_name":"Marija Zdravkovi\u0107","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ac6a53a0cb435d9900ee3984d153ae45/5E5C08E3/t51.2885-19/s150x150/66851574_2354137708132240_8606310035284295680_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"706629897","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"706629897","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ac6a53a0cb435d9900ee3984d153ae45/5E5C08E3/t51.2885-19/s150x150/66851574_2354137708132240_8606310035284295680_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"mylifeaskitty"}}}},{"node":{"id":"192815961","username":"gordongram","full_name":"Gordon Ramsay","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ca58f7dd68612bc05924e171092bcecf/5E579BFC/t51.2885-19/s150x150/26429265_1801356139896204_7973709430447407104_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":true,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"192815961","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"192815961","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ca58f7dd68612bc05924e171092bcecf/5E579BFC/t51.2885-19/s150x150/26429265_1801356139896204_7973709430447407104_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"gordongram"}}}},{"node":{"id":"19841914826","username":"vandometa_vlog","full_name":"","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2d6c970339a04d31010618e05763e7c1/5E431AE5/t51.2885-19/s150x150/67943135_2416133855379280_5169818650630160384_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"19841914826","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"19841914826","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2d6c970339a04d31010618e05763e7c1/5E431AE5/t51.2885-19/s150x150/67943135_2416133855379280_5169818650630160384_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"vandometa_vlog"}}}},{"node":{"id":"203227483","username":"tommycashworld","full_name":"TOMM\u00a5 \u20acA$H","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/130807ffdb11359677afba2220b62875/5E617C39/t51.2885-19/s150x150/73307916_1416703575159030_5557531713581613056_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":true,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"203227483","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"203227483","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/130807ffdb11359677afba2220b62875/5E617C39/t51.2885-19/s150x150/73307916_1416703575159030_5557531713581613056_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"tommycashworld"}}}},{"node":{"id":"4060156840","username":"fourtwenty","full_name":"Four Twenty","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2d1136bc4a84cfbf432655ebd98e4070/5E5EE4DC/t51.2885-19/s150x150/70594066_1368719939948662_8046432933394776064_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":true,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"4060156840","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"4060156840","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2d1136bc4a84cfbf432655ebd98e4070/5E5EE4DC/t51.2885-19/s150x150/70594066_1368719939948662_8046432933394776064_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"fourtwenty"}}}},{"node":{"id":"6945680089","username":"lazybrain.ai","full_name":"LazyBrain Initiative","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/8b8324c443ffe3de2aa80641d2a9a8de/5E5BE2F3/t51.2885-19/s150x150/27879053_781778075349644_4543882190064189440_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"6945680089","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"6945680089","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/8b8324c443ffe3de2aa80641d2a9a8de/5E5BE2F3/t51.2885-19/s150x150/27879053_781778075349644_4543882190064189440_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"lazybrain.ai"}}}},{"node":{"id":"17100242394","username":"lifewith_crohn","full_name":"Mb.Crohn","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/916a9a84175036f50a6a598457759364/5E41A323/t51.2885-19/s150x150/66674988_2903343653224618_4794522445505626112_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"17100242394","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"17100242394","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/916a9a84175036f50a6a598457759364/5E41A323/t51.2885-19/s150x150/66674988_2903343653224618_4794522445505626112_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"lifewith_crohn"}}}},{"node":{"id":"5355293","username":"andy_mann","full_name":"Andy Mann","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/e12200c75630dc7e1b2b814cb649d066/5E5E764B/t51.2885-19/s150x150/68862836_2425862274407548_3512337272991121408_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":true,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"5355293","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"5355293","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/e12200c75630dc7e1b2b814cb649d066/5E5E764B/t51.2885-19/s150x150/68862836_2425862274407548_3512337272991121408_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"andy_mann"}}}},{"node":{"id":"47457589","username":"istrahinja","full_name":"BJJ SANDMAN","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/a378f9ecc6df8cd0018088b6cfc5a5f5/5E4FC0A4/t51.2885-19/s150x150/19051700_1874715819469896_2141424226534424576_a.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":true,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"47457589","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"47457589","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/a378f9ecc6df8cd0018088b6cfc5a5f5/5E4FC0A4/t51.2885-19/s150x150/19051700_1874715819469896_2141424226534424576_a.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"istrahinja"}}}},{"node":{"id":"2158133293","username":"mpudja21","full_name":"\u041c\u0438\u0445\u0430\u0438\u043b\u043e \u041f\u0443\u0452\u0430","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/881e653f5d56d596133577f67a4a7e11/5E55C1F8/t51.2885-19/s150x150/74693418_1328190550698806_2099261116705144832_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"2158133293","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":1572031575,"owner":{"__typename":"GraphUser","id":"2158133293","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/881e653f5d56d596133577f67a4a7e11/5E55C1F8/t51.2885-19/s150x150/74693418_1328190550698806_2099261116705144832_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"mpudja21"}}}},{"node":{"id":"3998316794","username":"nature","full_name":"Nature","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2a4e907cbba6de380a178c95bc560376/5E49D099/t51.2885-19/s150x150/37130772_219216685405605_2651841279612157952_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"3998316794","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"3998316794","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2a4e907cbba6de380a178c95bc560376/5E49D099/t51.2885-19/s150x150/37130772_219216685405605_2651841279612157952_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"nature"}}}},{"node":{"id":"1808243250","username":"dobrotamilica","full_name":"\u13a0\u273a\u266d\u053b\u273a\u0442\u0394","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2b00e1cb3545393726a0bf1236e6c678/5E5DA2A1/t51.2885-19/s150x150/68908088_518976712264164_5633269725225025536_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":true,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"1808243250","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572114311,"seen":1572114311,"owner":{"__typename":"GraphUser","id":"1808243250","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2b00e1cb3545393726a0bf1236e6c678/5E5DA2A1/t51.2885-19/s150x150/68908088_518976712264164_5633269725225025536_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"dobrotamilica"}}}},{"node":{"id":"8738182557","username":"majceezmajcee","full_name":"Marija Palurovic","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ec6c203798b97791d8b0b6f1184f30d1/5E4FFACB/t51.2885-19/s150x150/57198888_414265232485760_3344820921243271168_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":true,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"8738182557","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"8738182557","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ec6c203798b97791d8b0b6f1184f30d1/5E4FFACB/t51.2885-19/s150x150/57198888_414265232485760_3344820921243271168_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"majceezmajcee"}}}},{"node":{"id":"1637485905","username":"niinamilanovic","full_name":"\u2664 N I N A \u2664","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ade9b86fc3985e70ab7c367b2cae4a26/5E50F5B7/t51.2885-19/s150x150/46354126_723371064697517_1754088811467374592_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"1637485905","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"1637485905","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ade9b86fc3985e70ab7c367b2cae4a26/5E50F5B7/t51.2885-19/s150x150/46354126_723371064697517_1754088811467374592_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"niinamilanovic"}}}}]}}},"status":"ok"}
EDIT 2:
Cookies were the problem, thats I was getting an empty JSON.
But I still dont know why
console.log(json_obj.node.username);
does not work.
It gives me this error:
Uncaught TypeError: Cannot read property 'username' of undefined
at :10:32
EDIT 3:
I've solved it with this code:
for (const node of json_obj.data.user.edge_follow.edges)
console.log(node.node.username);
in the json you posted above there is no tag username. To get to the user name you need to do json_obj.node.username
XMLHttpRequest is asynchronous, meaning that calling .open() and then .send() doesn't mean the request is complete after these calls.
Instead, you should set a function to the .onreadystatechange property like this:
var Httpreq = new XMLHttpRequest();
Httpreq.onreadystatechange = function() {
var json_obj = JSON.parse(this.responseText;
console.log(json_obj.username);
};
Httpreq.open("GET",yourUrl,false);
Httpreq.send(null);

getJSON returning 400 bad request using a body parameter, but not path variable parameter

When trying to call getJSON like so
<script>
$("#version").keyup( function() {
var jarName = $("#artifactId").val();
var jarVersion = $("#version").val();
$.getJSON("/xx/yy/zz/"+jarName+"", jarVersion, function(completion) {
$("#version").autocomplete({
source: completion
});
});
});
</script>
with a back end like this
#RequestMapping(value = "/xx/yy/zz/{jarName}", method = {
RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public List<JsonNode> getVersionsAssociatedwithJar(#PathVariable String jarName, #RequestBody String version) {
I get a 400 bad request error and the url looks like so
xx/yy/zz/jarName?131.31
with the 131.31 what I am typing in on the front end and what I want as the "version" Param. Am I making a simple mistake with the request mapping? I've never used spring before but am finding it difficult to debug since it doesn't even get to the back end
Logger on Back end gives this message
[nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.util.List<com.fasterxml.jackson.databind.JsonNode> com.lendingclub.macgyver.dependency.AppDependencyApiController.getVersionsAssociatedwithJar(java.lang.String,java.lang.String)
Perhaps try changing the {jarName} to be a regex (i.e. {jarName:.+}), since there are known issues with the last #PathVariable containing dots.
Also adding TRACE logging on org.springframework.web can be useful as you will see the request coming in and any issues with it.

How to add an API Key to a UrlFetchApp in Google Apps Scripts

Solved
Thanks to Dimu Designs for helping out.
The following works.
function myFunction() {
var url = "https://api.fortnitetracker.com/v1/profile/pc/Ninja"; var apiKey = "xxx-xxx-xxx";
var res = UrlFetchApp.fetch(
url,
{
"headers":{
"TRN-Api-Key":apiKey
}
} ); var content = res.getContentText(); Logger.log(res); Logger.log(content);
}
Problem
I'm trying to use Google App Scripts within Google Sheets to call the external Fortnite API to request player data. The bit I'm stuck on how to add an API Key as a header when passing the request.
This is what I've built so far (please no laughing)!...
function myFunction() {
var res =
UrlFetchApp.fetch("https://api.fortnitetracker.com/v1/profile/PC/Ninja?");
var content = res.getContentText(); Logger.log(res);
Logger.log(content);
}
When I try to run this I get back the following error:
Request failed for
https://api.fortnitetracker.com/v1/profile/PC/Ninja? returned code
401. Truncated server response: {"message":"No API key found in request"} (use muteHttpExceptions option to examine full response
I've tried adding my API key in a number of ways based on a number of different posts, but it doesn't work and just confuses me even more (easily done at this point).
Does anyone have any idea how I could go about completing the script to ensure I get information back? :)
---Edit---
First of all, thanks for the help guys, here's where we are at the moment. I've now tried the following:
var url = "https://api.fortnitetracker.com/v1/profile/pc/Ninja"; var apiKey = "xxx-xxxx-xxx";
var response = UrlFetchApp.fetch(
url,
{
"headers":{
"TRN-Api-Key":apiKey
}
} );
Rather than a 401 error, this time a 403 error is being returned.
Note, I've also tried to authenticate the header using "basic" but that doesn't work".
EDIT
I suspect that the API uses a custom header. When you register for an API key you get a string in the following form:
TRN-Api-Key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
I'm guessing here, but the text preceding the colon appears to be a custom header and the string of characters after the colon is the API key.
Without proper documentation this is STILL pretty much a shot in the dark but you can try the following:
var url = "[FORTNITE-API-ENDPOINT]";
var apiKey = "[YOUR-API-KEY]"; // sans header and colon
var response = UrlFetchApp.fetch(
url,
{
"headers":{
"TRN-Api-Key":apiKey
}
}
);
Also, make sure to check out the UrlFetchApp documentation for future reference:
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

Accessing a Javascript variable inside an HTML file

I'm doing a little bit of reverse engineering on the Rapportive API in Gmail.
I make this request
import requests
url ='https://api.linkedin.com/uas/js/xdrpc.html'
r = requests.get(url)
print r.text
The response is an empty HTML file that has a lot of Javascript in it. On line 3661, it sets the RequestHeader for the subsequent call to Rapportive:
ak.setRequestHeader("oauth_token", ae);
Is there a way I can request that page and then return ae?
I think you can try:
Get the page as you already does;
Remove all non-javascript elements from the response page;
Prepend a javascript (described below) in the page's javascript to override some code;
Execute it with eval('<code>');
Check if the token has been set correctly;
I'm proposing the following code to override the XMLHttpRequest.setRequestHeader functionality to be able to get the token:
// this will keep the token
var headerToken;
// create a backup method
XMLHttpRequest.prototype.setRequestHeaderBkp =
XMLHttpRequest.prototype.setRequestHeader;
// override the "setRequestHeader" method
XMLHttpRequest.prototype.setRequestHeader = function(key, val)
{
if ('oauth_token' === key)
headerToken = val;
this.setRequestHeaderBkp(key, val);
}
If you are just interested in retrieving the token can't you just do a regex match:
var str = '<script>var a = 1;...ak.setRequestHeader("oauth_token", ae);...</script>';
var token = str.match(/setRequestHeader\("oauth_token",\s*([^)]+)/)[1];
Although this assumes ae is the actual string value. If it's a variable this approach wouldn't work as easily.
Edit: If it's a variable you could do something like:
str.replace(/\w+\.setRequestHeader\([^,]+,\s*([^)]+)\s*\);/, 'oauthToken = \1';
Before running the JavaScript returned from the page, then the global oauthToken (notice the missing 'var') will contain the value of the token, assuming the the evaluation of the code is run in the same scope as the caller.

Tasker Exported App Javascriptlet Error

I have created a Task to get the JSON result from 500px using HTTP GET and then used Javascriptlet code:
var arr=JSON.parse(global('HTTPD'));
var name=arr.photos[0].name;`
To parse the JSON result, everything is working fine when run from the TASKER.
But after exporting it as an APP, it gives error on JSON.parse():
Uncaught TypeError: undefined is not a function
I tried using .js file also. Can't figure out what am I missing?
I would use javascriptlet in following way.
url = "your url here.";
http = newXMLHttpRequest();
http.open("GET",url,false);
json = eval("("+http.responeText+")");
yourvariablename = json.var[0].name;
Also i do an variable set before the javascriptelet task.
for example. #variable set to ...
then set that #variable to some value within json.

Categories

Resources