Intercept and edit a request body/payload using an userscript XMLHttpRequest - ajax

Couldn't find the answer for this while using an userscript.
Is it possible to modify a request body inside an userscript in tampermonkey for example?
I could patch the open function to modify type and url, but I couldn't access the request body

Related

How to remove request body from subsequent requests in JMeter

I tried to create a test script in JMeter where I log in by using API. I created a test script by BlazeMeter chrome extension.
Some of my API calls fail because I am getting CloudFront error. I found out the issue is that GET requests contain a request body. GET requests cannot contain a request body on CloudFront. When I tried the same get requests in postman without request body, I didn't get any errors. I got the same error in postman if I added a body request. This way I can confirm this is the issue.
I am looking for a way how to remove body request from subsequent GET requests in JMeter to get rid of this error.
This is the original API call (sampler) with POST login api request:
After the POST request there is a redirection to a page that makes other GET api calls to fetch static files such js files, etc. The problem is that these GET requests also contain the same request body as the original POST call.
As you can see I can fetch statis files if I remove body request:
I tried to add preprocessor and postprocessor for original POST request, but I don't know how to access subsequent GET requests. I can access only original POST request:
Is there a filter or a way how to remove body request from GET requests?
I don't think it's possible, at least not with JMeter 5.5
If you take a look at HTTPSamplerBase source code you will see that the request body is being copied fully from the original HTTP Request sampler:
this.sampler = (HTTPSamplerBase) base.clone();
So the options are in:
Raise an enhancement requests via JMeter Github Issues
Amend HTTPSamplerBase code to remove the request data from generated AsyncSamples
Handle embedded resources download manually using Parallel Controller

Intercept HTTP Request in page

I am trying to make a scraper for a page of a supermarket. I noticed that this supermarket make API HTTP calls via AJAX, and if I enter to Inspect > Network then I can see the request headers of the calls I need.
Inside the requests headers there is an X-Token. If I use this X-Token with the URL via Postman I can get all the info in JSON format, which is better than scraping the web.
The problem is that this X-Token expires (I think, it still works). Is there any possibility to make a call to the page and "intercept" this API call in order to retrieve this X-Token and use it for the next custom requests?
I'am using Ruby on Rails :)

Is there any way in django to distinguish if it is a normal browser request or ajax request

I am using django and making some ajax request to server. As the url is visible in javascript someone could easily copy that and start making request via url bar. Is there any way in django that we can distinguish that the coming request is sent by ajax not a regular browser reqeust.
You can use a tag in your ajax,and in code check request from
Yes you can use
HttpRequest.is_ajax()
as in documentation
https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.HttpRequest.is_ajax

Unable to add custom header to response

I am unable to add a custom header to a response that is returned from render():
response = render(request, 'my_template.html', {'ctx1': 1, 'ctx2': 2})
response['My-Custom-Header'] = 'abc12345'
return response
This is a response to an Ajax request initiated using jQuery's $.get(). On Chrome, the response has the template rendered properly, but it does not have the custom header. If I print the response object before returning, I see that it has my custom header.
I thought the issue was because of this answer, so I added Django middleware to add a header to all responses:
class CustomHeaderMiddleware():
def process_response(self, request, response):
response['Access-Control-Expose-Headers'] = 'My-Custom-Header'
return response
All my responses now have header Access-Control-Expose-Headers: My-Custom-Header, but I still see this issue.
My request is local; I'm using the Django development web server.
If I send a non-Ajax GET request, whatever custom header I add in Django is visible in the response on Chrome. So this issue seems to be limited to Ajax requests.
I am using Django 1.11.4 and Python2.
[edit]
If I capture the response in WireShark, I see that it does not have My-Custom-Header. Furthermore, if I add a custom header to the response dictionary in the above middleware, the header shows up in Wireshark and is visible in Chrome. So this seems to be a Django issue with responses to Ajax requests.
This issue is unrelated to Ajax. I was calling my view using this template tag. The custom response headers are lost because of line 32 in that code.

Not able to fetch headers via AJAX calls

I have added the following to server-side API.
header('Access-Control-Allow-Headers: temp');
header('Access-Control-Expose-Headers: temp');
header('temp: 12345');
When I am making a cURL request to the API, I get the headers along with the response-data.
But when I am making an AJAX call to the same API, I only get the data, without the header.
Thanks in advance.
Regards,
Anish
You can retrieve the headers in an AJAX request by calling the getAllResponseHeaders() method on the XHR object. The reference is here

Resources