How to send a HEAD HTTP request in Python to Amazon S3? - amazon-s3

I'm trying to retrieve HTTP HEAD information from a S3 resource using Python. Unfortunately the response always returns a 403 error. I am using code that was suggested
here but unfortunately this does not work for S3 requests. Here is the code (host and key details omitted):
>>> import httplib
>>> conn = httplib.HTTPConnection("www.mybucket.us")
>>> conn.request("HEAD", "/mykey?myparamterers")
>>> res = conn.getresponse()
>>> res.status
>>> 403
The request also sends a signed expiration as part of the query string.
I have also tried using httplib2 but the HEAD REQUEST simply hangs.

If you are using a signed URL, the URL is signed by method (e.g. HEAD or GET). So you will need a different URL for each method.

403 tells you that the request sent was valid (whether it was correct is another story), but that you don't have permission to access the requested page for some reason. At least you know that a valid HTTP request is being sent.
Can you check the server log at all? That might help shed some light on the problem...
I'm not sure about the "HEAD" request either. Can you not use "GET" or "POST" and extract the header yourself? It might be that "HEAD" is not implemented by the library... I am not sure - the documentation I've been able to find by quick googling is woefully inadequate.

403 HTTP code means forbidden. Probably, site administrator disabled this method.
try telnet
telnet www.mybucket.us 80
HEAD http://www.mybucket.us/mykey?myparamterers
Host: www.mybucket.us
<ENTER>
<ENTER>
and watch for server response.
Alternatively, you could use conn.set_debuglevel(1) in python code.

A HEAD request on an AWS S3 pre-signed URL generated for GET will result in a 403 Forbidden response. You can, however, achieve the equivalent of a HEAD request on a pre-signed AWS S3 URL by doing a GET request with the Range header set as follows:
Range: bytes=0-0
here is a code snippet
import requests
response = requests.get(download_url, headers={"Range": "bytes=0-0"})
As shown in the Screen shot, response will have enpty body but will have Hearder info.
https://i.stack.imgur.com/9J99G.png

Related

Same HTTPS request works in Postman but gives 400 Bad Request in JMeter

I think I tried all the possible combinations of headers and body for my POST request in JMeter but is giving me 400 Bad Request saying that the languagePairID parameter from the body is invalid even tho it is exactly same as in Postman.
My headers:
And the request:
Meanwhile in Postman in works perfectly fine:
I tried checking and unchecking the multipart/form-data and browser-compatible headers, but nothing seems to help. I read that using Wireshark might be useful in such cases but I think for this one is not possible as it is HTTPS. I will be very grateful for any advice.
Uncheck "Use mutipart/form-data"
Remove everything from the "Body Data" tab
Switch to "Parameters" tab and put your parameters there:
Going forward be informed that you can easily convert your Postman script into JMeter:
Start JMeter's HTTP(S) Test Script Recorder
Import JMeter's certificate to Postman
Configure Postman to use JMeter as the proxy
Run your request in Postman
JMeter will capture the request and generate relevant HTTP Request sampler and HTTP Header Manager
More information: How to Convert Your Postman API Tests to JMeter for Scaling

What is the difference between a postman request and a request from heroku or localhost

I can make a request from postman but when I make the same exact request (I'm talking about even copying the code from postman) I get an error.
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://glacial-stream-35306.herokuapp.com' is therefore not allowed access.
Even with the cors-eveywhere chrome extension and attempting to use jsonp I cannot get it to work. Even though the request goes through every time on postman.
What makes a postman req different from a request from a Heroku app?

Log response body apache with the client IP

I need to log the server response body along with the client which requested it.
Tried mod_dumpio which logs the response body but it doesn't reveal
the client IP so I am not able to map the request and response.
The other option was audit log which seemed the exact tool I needed,
but it doesn't support logging actual response body as
yet.
Could anyone please suggest me an appropriate solution?

Pre signed url PUT does a GET request in the background?

uploading a image/jpeg to s3 bucket with PUT works fine, but just before the put the javascript throws an error saying it tried to do a GET using that signed key anT request in the first place. When i check the network in the browser it shows an OPTIONS method call with 200 status OK and then a PUT call using the signed url with 200 OK and no GET calls. Still the console throws the GET error.
Any insights would be appreciated.
Got the answer in the following post: Why am I getting an OPTIONS request instead of a GET request?
Before every PUT request there is an OPTIONS ajax request which does a GET for that key. Apparently these PUT cross origin request are preflighted with OPTIONS call

PUT/POST request in SOAPUI giving 403 forbidden, while same request working fine in rest client Postman

There is no authentication on server side so authentication should not be issue.
URL format: PUT
https://localhost/api/v1/protections?integrationKey=111&userKey=1111&group=111&category=foo
Payload:
{"action":"BLOCK"}
This is working fine in Postman.
In SOAP UI , I am giving input as under:
EndPoint: https://localhost
Resource: /api/v1/protections
Parameters:?integrationKey=111&userKey=1111&group=111&category=foo
in Media type, I am selecting "application/json"
and entering {"action": "BLOCK"} but getting "Wed Jan 20 16:25:27 PST 2016:DEBUG:Receiving response: HTTP/1.1 403 Forbidden
"
Is there any suggestion to get the output in SOAP UI.
Depending on the server where the rest is exposed service generates an HTTP 403, you should verify that server is and thus find the fastest response.
Also try making a GET request from the browser to see if you can answer correctly because problem lock your machine to the server.
As is https, it may be that you lack some certificate set SOAPUI. possibly Postman you use already has configured. Try to check this setting.
In my case, I missed the Header "User-Agent" and "accept". I put in Soap UI and Works.
In Postman, this headers it put automatically.