I have a WCF Web Api endpoint that returns an invoice: http://localhost/api/invoice/23
The format it returns is that of the accepts header in the request. If the Javascript wants JSON or XML then it just sets this in the accept header. This is how WCF Web Api seems to work. I've added a PDF formatter to invoice so that when asking for application/pdf it will get a rendered pdf file stream back with the appropriate MIME type. This works fine and I can test it in fiddler.
I need the user to click something in the browser to start the PDF download and pop up the open/save dialog. I don't know how to do this and set the accept header of the request. Static links or window.location in javascript won't work because it doesn't let me set the header. AJAX request won't work because although I can set the header, it expects text back and it won't show as a download in the browser.
I'm not sure how I can do this. Any suggestions would be greatly appreciated.
You can just dynamically create a form in JavaScript and ask it to start in a new tab. That should give you what you want.
function SubmitRequest()
{
var myForm = document.createElement("form");
myForm.method = "post";
myForm.action = "url here"
var myInput = document.createElement("input");
myInput.setAttribute("name", "json");
myForm.setAttribute("target", "_blank");
myInput.setAttribute("value", "Your value here");
myForm.appendChild(myInput);
document.body.appendChild(myForm);
myForm.submit();
document.body.removeChild(myForm);
}
The easiest way is to add an A tag in your page with a link to http://localhost/api/invoice/23.pdf and then use the AddUriPathExtensionMapping on your formatter to have it generate the accept header automatically from the path extension on the URI.
Related
I'm working on a small-scale instagram scraping project mainly using selenium and the python requests module. I discovered that when the Request Header changes for www.instagram.com, the text that I get from using
requests.get("https://www.instagram.com/<address>/?__a=1")
Returns the HTML code for the webpage. Instead I was expecting it to be the json text containing post details. Currently, it works fine if I change the headers manually.
How do I automatically get the request header using selenium or requests? I'm expecting to get the text labeled in the image attached:
www.instagram.com Request Header
Thank you.
import requests
response = requests.get("https://www.instagram.com/<address>/?__a=1")
print(response.status_code)
print(response.headers)
print(response.json())
As I see in the headers printed out on to console, there is a set-cookie header. If this is something that you are looking for, then you may use this line to extract only this header:
print(resp.headers['Set-Cookie'])
I have an API which redirects to browser and on that we have to enter username and password.
The API returns the HTML page as part of the response. How from HTML response we can pick username and password via locator Id and click button?
I have tried below but as it is returned in the response I somehow need to tell where in response find that field and input.
And input('#username', 'username')
And input('#password', 'password')
When click('#kc-login')
What I would do is scrape any information needed out of the HTML like this: https://stackoverflow.com/a/61605535/143475
And then form an HTTP request to do what the clicking of the button actually does. Remember no matter what HTML and complex JS / UI you see, finally everything becomes some HTTP request. Use the developer tools "network" part of the browser to figure this out.
The rest is up to your creativity. Work with some web-developers if required.
I'm using Apache Wicket and I have following problem:
Inside a onSubmit() method I am sending a POST request to external web address with Apache httpClient. As a response I get html (inside my response object).
How can I get Wicket to render this html in browser?
So basically what I'm trying to do here, is simply what would normally happen if I submitted a html form to this web address. However for security reasons I don't want to give user pages containing forms that contain this data I'm trying to send.
You can get the response via getResponse() in any component. (I assume the onSubmit() is on a form).
How about something like:
getResponse().reset();
getResponse().write(htmlPage);
htmlPage should be a CharSequence containing the html page to be rendered.
I'm working on my first chrome extension, and using an image-based context menu item to capture the URL of a given image, and want to then display that image at a specific URL in a new tab. So, I need to pass the URL of the image clicked on (using srcUrl) to a specific script that can then render it on that page. Is it possible to perform an HMLHttpRequest from within a chrome.tabs.create() call, or must this be done some other way?
Thanks for any help.
You would need to create an HTML page containing that script and put it into your extension folder. Then you can just pass image url to it as GET parameter:
chrome.tabs.create({url: "local.html?img_url=...");
If url parameter is not enough, you would be also able to communicate with that page using chrome.tabs.sendRequest():
chrome.tabs.create({url: "local.html", function(tab){
chrome.tabs.sendRequest(tab.id, {img_url: "local.html?img_url=...");
));
With the request listener in that page:
chrome.extension.onRequest.addListener(function(request) {
console.log(request.img_url);
});
I have created a JSP page, which will accept parameter. Once the page received the parameter, it will return an XML to user.
I want to create a VB program, that will display a form and ask user to enter the value of the parameter, and then will pass it to the JSP page, and get the return XML and display it to user in VB program.
Is it possible to do so?
Thx
Use the HttpRequest class to request a web page. Then just manipulate the URL to add query string parameters. If you need to do this via a POST request (versus GET), write the parameters in the body.