Not able to fetch response URL in APIGEE - apigee

Heloo
I am trying to get the response url from the backend. i am able to get request url, but not able to get response URL. Any suggestions/ ideas are mush appreciated.Thanks in advance.

Assuming your backend sends a redirect URL (as HTTP 302 Location Header) and the URL contains an auth code that you want to extract in the Apigee proxy-flow response path -
You can use a java script policy in the Response path of your proxy flow, like below:
url = context.getVariable("response.headers.Location");
var re = new RegExp("#.*[?&]" + "code" + "=([^&]+)(&|$)");
var code = url.match(re);
context.setVariable("authcode", code);

Related

python request return 401 while the browser is works well

I want to use the python request to access the below url, while I get the 401. while I can open the below url in the website. I don't know how to use the python request to access it.
https://data.10jqka.com.cn/funds/gnzjl/field/tradezdf/order/desc/page/2/ajax/1/free/1/
my code:
import requests
response = requests.get(url, headers=self.headers)
What I should do and could anyone give a code example to access that url?

while hitting an API Request throws 302 status code how to solve this

I'm hitting an API using http dart library while doing this I'm getting 302 status code. I know that 302 status code is for redirects, can you please say how can i enable redirects in http post method. I have used the following code:
Future<LoginModel> login(String username, String password) async {
var client = new http.Client();
final response =
await client.post(LOGIN_URL +"username=Student&password=2018" ,
headers: {'Content-type': 'application/json',
'Accept': 'application/json'});
if (response.statusCode == 200) {
return LoginModel.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load post');
}
}
302 is a status code returned by the server to indicate that the client should retry the request using a different URL. It's a way to redirect the client to a different endpoint.
The problem is that only GET (or HEAD) requests can be retried automatically. You are using a POST.
If the 302 status code is received in response to a request other than
GET or HEAD, the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.
A well-behaved API should probably not issue 30X in response to a POST, but it is. The way to get around this is to make a new http request with the redirected URL. (You might want to put it in a while loop to keep following the redirects until you get to 200, or some error, or reach a timeout/limit.)
add header with
"Accept":"application/json" . henceforth it will only return json data otherwise it will prompt to redirect with html url, default dio configuration does not have response type so you have to mention it in header.
This is because of some issue with the URL that you use. status code 302, says that the URL you are using has been moved or redirected.
May be the loginurl that you are using has certificates now and using https:// instead of http://

How to get actual request URL in routed service when using Zuul service and Spring boot?

I am using zuul service as API gateway for all my micro services.
In zuul filter, I am able to get the requestUrl like below:
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.context.RequestContext;
.....
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String requestUrl = request.getRequestURL();
Here requestUrl: http://localhost:8052/api/userservice/users
In user service, when I am trying to get the request URL in spring boot rest controller using HttpServletRequest:
String requestUrl = request.getRequestURL();
Here requestUrl: http://localhost:8055/userservice/users
I am getting the routed service request url but NOT actual URL which client requested.
How to get actual request URL in routed service ?
You are getting the routed service because when you call the getRequestURL method after the request url has been modified. Calling the same method in a pre filter which order is -50(for example), you can get the actual URL zuul server received.
So far I don't find any method to get the actual URL in a route/post filter after the request is modified, I think one way to solve this problem is define a pre filter to get the actual URL before modified, and put the url into a Threadlocal field, then you can get the url in the route/post filter by accessing the field.
Zuul creates a new Http Request to user service. obviously with different URL.
So in the user service its impossible to get access to original request by design.
Zuul is supposed to encapsulate an original request, not reveal it.
All headers, request parameters, request body should be preserved, it's only a path to be changed.
If for some reason there is a need to get parameters from original request, in Zuul filter they can be extracted and set as Headers to the "new" request before it's sent to user service.
Then in UserService there should also be a filter that will "read" these headers and use the information as required

Google Fit Api 403 Error from remote client

I have a web app hosted on IIS 7 that is doing Http calls using the Google Fit API, I'm able to successfully send a POST and retrieve an access token, after which I do a GET for the following uri:
"https://www.googleapis.com/fitness/v1/users/me/dataSources/raw:com.google.weight:com.google.android.apps.fitness:user_input/datasets/00-1427209862000000000"
Here's how I build a request and look at the response:
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.ContentLength = 0;
request.Accept = "application/json";
request.Headers.Add("Authorization", "Bearer " + dict["access_token"]);
response = (HttpWebResponse)request.GetResponse();
respStream = response.GetResponseStream();
sResponse = new StreamReader(respStream).ReadToEnd();
respStream.Close();
Response.Write(sResponse);
When I run this app on a browser on the host server, I successfully get a json object (it isn't the json I expect, but that's another issue). However, when I try to access the site on a remote client, I get a 403 error pointing to when I try to retrieve the response. Any ideas?
It probably depends how you got the access_token value.
How long after retrieving the access token are you making this request? It's only valid for an hour, so you may need to fetch a new one using the refresh token.
There's some more resources on access/refresh tokens this question:
Google OAuth2 Refresh_token expires when Access_token does
Update: This turned out to be a permissions issue. I was trying to access a data source that I didn't have scopes for, the fact that it didn't return a 403 when accessing on the host threw me off the trail. Still strange that it didn't return a 403 (it just returned an empty json object) when requested on the host server though...if you see this and have an idea why, feel free to comment. I'm curious.

Servlets sending request to URL?

I am totally new to servlets.I am trying to send a request to google to get a token.I tried many sources but all i can get is "send request to a url with some parameters". My question is how can we send request to a url by passing few parameters using servlets..please help..
You can send parameter along with url using Query String. A URL with Query String will look like this:
http://google.com/search?a=b&c=d
Where a & c are parameters which will have b & d values respectively.

Resources