Batch Paged Requests - json

I need to retrieve the news feed for a user, and using the Graph API that returns multiple pages. I'd like to get four pages, and that's pretty slow, so I'd like to batch a request for all pages into one request using batching. I can't figure out how to batch-request multiple pages - clearly each request in the batch is dependent on the previous.
I wrote up a webpage to let me test this all out, containing the following form:
<form method="GET" action="https://graph.facebook.com">
<input type="hidden" name="access_token" value="blahblahblah">
<input type="hidden" name="batch" value="[{'method':'GET', 'name':'getnews',
'omit_response_on_success':false, 'relative_url':'me/home'},{'method':'GET',
'relative_url':'{result=getnews:$.paging.next}'}]">
<input type="hidden" name="method" value="post">
<input type="submit">
</form>
Of course, when I get a response from Facebook that requires paging, the paging.next value is a full URL and the batching functionality wants a relative_url, so my first request works and my second request returns with the paging.next URL in a "body" key.
I found a piece of facebook documentation which states that a request like the following works, where you graph.facebook.com is followed by a full URL specifying a request://graph.facebook.com/http://graph.facebook.com/me/home?_fb_url=me/home&access_token=blahblahblah"
I was surprised to find that this works, but it does when I just make that GET request to the Graph API. Unfortunately, the batching functionality does not allow me to put that full URL in the "relative_url" field - it just does that "body" thing.
Does anybody have a good way to batch requests for multiple pages? kongo09 and I were wondering this over in the facebook dev forum, but I guess that's on its way out... http://forum.developers.facebook.net/viewtopic.php?id=107098
Thanks,
-Karl

I have found a way:
You should use
"relative_url":"me/home?after={result=getnews:$.paging.cursors.after}"

Related

Replicate Google's "I'm Feeling Lucky" Button On My Own Site -- To Take User to Top Search Result Automatically

I am trying to replicate Google's I'm feeling lucky button.
But when I click the button it takes me to " Redirect Notice" page.
My code is as below:
<form action="https://google.com/search">
<input type="text" name="q">
<input type="submit" value="Google Search">
<input type="submit" name = btnI value="I'm Feeling Lucky">
</form>
TLDR: Just use DuckDuckGo. You typically don't care about what search engine is doing the redirecting, so long as you land on the top-result for a search term and the page for that result loads (and not a redirect page of the search engine).
https://duckduckgo.com/?q=%5Csoybeans
This URL above will redirect to...
https://en.wikipedia.org/wiki/Soybean
The %5c code above is the indicator for loading the page directly without search results (similar to the BtnI of Google).
Details on the Redirect Page from Linking to Google's "I'm Feeling Lucky"
Oct. 2, 2019: The redirect page was first reported on Google Search Help, the question has since been closed and comments are disabled -- they are probably not going to fix this.
You cannot use an <iframe />. Loading <iframe src="https://www.google.com/" /> has always loaded blank for me for the past ten years.
Likewise, you cannot use XMLHttpRequest, which I just tried, because CORS will block you out, and most browsers do not allow XMLHttepRequest.setHeader("origin", "http://www.google.com/");, otherwise, you could simply get the redirect page, parse for the first URL, and load that page. We know this is the case, because doing this following in a terminal will bypass the redirect page (thanks to Metamorphic for discovering this):
w3m -header "Referer: http://www.google.com/" "https://www.google.com/search?btnI=1&q=soybeans"
Iframes don't work, XmlHttpRequest doesn't work, and the main post on the Google Search forums for this topic has been closed and locked. I think it's obvious you'll need to find a different approach -- i.e., like a different search engine.
The %5c code above is the indicator for loading the page directly
without search results (similar to the BtnI of Google).
I've been looking all over the place for this, thank you!

How to find out what url HTML button submits to

So I'm scraping a website (instacart.com) and it requires a zip code to determine what data it displays. I want to use Python requests to post an arbitrary zip code. The only problem is I don't know what url to post it to and whether it requires any other arguments like an authenticity token or a user cache key. The zip code is entered via an text box that looks like this:
<form data-radium="true">
<input id="postalcode-16749"
name="postal_code"
type="text"
aria-invalid="false"
aria-describedby=""
autocomplete="on"
placeholder=""
data-radium="true"
value="" style=(super long block of css stuff)>
</form>
and then posted via a button that looks like this:
<button type="submit"
data-radium="true"
style="touch-action: manipulation; (long block of more css)">
Continue
</button>
I don't know a lot about web programming, but I was taught in school that HTML forms would look more like this: <form action="/action_page.php" method="get"> and you could use the action attribute to find where it was posting to. Is there a way to use the developer console to find what I'm looking for? How can I post a zip code to this website with Python?
Edit: I did a little more digging and I found that the request payload is {"current_zip_code":"some_zip_code"}, and that it's actually not using POST, it's using PUT. There's still a problem though, the request url looks like this: https://www.instacart.com/v3/bundle?source=web&cache_key= and then there's a different code each time for the cache_key. How do I know what url to post to?
I'm posting this answer in case anyone tries to do a similar thing. I found the url the button posts to and its parameters by looking in the network tab of the developer console and clicking the button. Then I ran into the problem that the url it sends the PUT request to changes every time, always ending in a different cache_key.
The solution was to use a python module called seleniumwire to simulate a browser and then grab all the network traffic. From there I looped through it and found urls containing cache_key= and stored everything after that as a string. Then tacked that string to the end of this url: https://www.instacart.com/v3/bundle?source=web&cache_key= and went back to using requests.
hope this helps someone!

HTML Form Downloading Response of POST Request

I am trying to use an HTML form to execute a post request. I know that in general AJAX is probably a better way to go about this, but my use case is very simple and I have to upload a file in this form submission (which is really easy just using HTML forms). Anyways, everything works, but for some reason my browser is downloading the contents of my POST request response, which I do not want to happen. I want nothing to actually occur when I submit the post request other than the post request being sent out.
Here is the HTML portion of my code:
<form enctype="multipart/form-data" action="/action" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000"/>
<input type="text" name="email" id="email">
<input type="file" name="file" id="file" accept="image/*">
<input type="submit">
</form>
I am not sure that it is relevant, but I am using a Flask webserver and here is the response I am returning:
response = {"status": 200}
return Response(json.dumps(response), mimetype='applications/json')
The issue is that I keep having files downloaded from my browser with '{"status": 200}' in them.
Status : 200 indicates.
The request has succeeded.
So an OK status response is sent to the location, /action as you have mentioned in action="/action". So I'm guessing this is the view you wanted to send the POST request too.
I don't have the code for your /action view, but either :
This is not the view you intended to send it too.
It is the view you intended to send it too.
So if its not the correct view, what you can do is, use jinja templating and mention your view like this.
<form method="POST" action="{{url_for('xyz')}}">
Notice how I use the url_for() in jinja templating to specify route.
And if its the correct view, i can answer the question by seeing your code for the view called action.
Most likely what could be wrong though is,
your return function is erroneous. Check the return function again.

What happens when submitting an html form? (the process behind the scenes)

Can someone please tell me what happens behind the scenes in the following case (i.e. explain the whole technical process)?
<form method="get" action="#">
<input type="text" name="d" value="flowers">
<button type="submit">send</button>
</form>
In this case after one has clicks on “send” a new webpage opens saying: "You have searched for "flowers" " and an image of some flowers below.
In the browser tab right after the URL of the newly opened page there is
“/?s=flowers”. What is that?
Thank you in advance for your answers!
When you click Send, the page data specified in the form information and values is passed to the server via HTTP.
The /?s=flowers is the GET data being passed back to the server. Although, based on the form code you've provided, the "name" of that value is d. So the URL would actually have /?d=flowers
The PHP or server side language then handles that information to do specific tasks. It can access the info using the name "d". This method of sending data is called GET, there are also other ways of doing this. The most common, POST, does not display the data in the URL and send the data through HTTP headers.
The code you've shown has an action of "#" which means the HTTP method is being sent the same page. Meaning this page code would have some PHP located in it. This can also be done by using a seperate file, such as action='send.php'

How to submit HTML form into a Sheet?

I've checked around and most things I've found are from 2012 and workarounds for a then-existent bug. Unfortunately I'm not understanding Google's documentation on this very well.
I have a script project that is serving a web page to visitors with an HTML form:
<form id="gradingform">
<input type="text" name="name" placeholder="Name">
<input type="number" name="grade" placeholder"100">
<input type="submit" onclick="<this is where I'm having issues>">
</form>
I believe that this needs to be handled like any other time getting a script while serving a web page - by using the google.script.run. With a form specifically, I think that it's supposed to be using a success handler, so for example, something along the lines of
google.script.run.withSuccessHandler(gradeSubmitted).recordGrades()
gradeSubmitted() would be a function that just dispays a message, easy enough by doing some easy div changing. What my real issue is what recordGrades() would be like.
How do I pass the form to this function, and how do I collect the information from the form? From there I will be adding it to a Sheet, which is easy enough once the information is in an array because I can just append it. The documents say the form information should be passed as a Blob, but Google's example is kind of confusing.
This is what you have to do
onclick="google.script.run.withSuccessHandler(gradeSubmitted).recordGrades(this.form)"
And in the code.gs file you will be receiving a json as
[16-03-25 10:51:51:046 IST] {grade=10, name=Anees}