Sending serialized data over POST request - python

First thing, I'm not very used to HTTP request, so bear with me if I make some stupid mistakes or assumption that are completely wrong.
I'm trying to send an image using a POST request using Flask, the code that I'm using is the one that can be found on this link:
https://gist.github.com/kylehounslow/767fb72fde2ebdd010a0bf4242371594
Basically it encodes the image using cv2, than sends it over in the POST request, I could get that to work but I wanted to send some more information, therefore I rewrote the request as:
payload = {'img':img_encoded.tostring(), 'name':'foo'}
response = requests.post(test_url, data=json.dumps(payload), headers=headers)
this gives me an error, more specifically:
TypeError: Object of type 'bytes' is not JSON serializable
This is due to the fact that I'm encoding the image, so I tried to just send the dictionary, without dumping it to a JSON, the request goes through, but now I dont know how to decode the data field in the request.
If I try to access to request.data I get this:
b'img=very_long_first_field_of_binary_info&name=foo'
How do I revert that to a dictionary?
Since I can't use json, I don't know what to do.
Thanks in advance for your help,
Mattia

Related

Sending straight JSON to API using requests?

The jist of my problem is that there is an open API that contains road info that I'm trying to pull very specific information from. The JSON request I'm trying to make looks like:
data1 = """
<REQUEST>
<LOGIN authenticationkey=""/>
<QUERY objecttype="RoadData" schemaversion="1.0">
<INCLUDE>SpeedLimit</INCLUDE>
<INCLUDE>RoadMainNumber</INCLUDE>
<INCLUDE>RoadSubNumber</INCLUDE>
</QUERY>
<QUERY objecttype="RoadGeometry" schemaversion="1.0">
<INCLUDE>Geometry.WGS843D</INCLUDE>
<INCLUDE>RoadMainNumber</INCLUDE>
<INCLUDE>RoadSubNumber</INCLUDE>
</QUERY>
</REQUEST>
"""
and I'm simply sending a request to the correct URL with this as my json code, i.e:
respones = requests.post(url, data=data1) but I'm only getting response 400 which leads me to believe my JSON request is wrong, however they supply an in-house console for testing your requests directly on their website and there it's working. Am I simply stupid here and I'm sending it incorrectly?
Tried looking at similar problems on stackoverflow and online but every single issue uses a dictionary to map their requests but that's not possible here? At least I don't know how to get the keyword in a dictionary as a condition since the sice of the dataset requires this.

is there any way to get HTTP response code without receiving html files in python?

I don't know maybe am asking some kind of weird question or not.
Normally if we use any module in python like requests or urllib3. We get a response for each request. we get status-code, cookies, headers, and the HTML content.
But the problem is! this HTML data is huge and I don't need this data. I just need a response code for a request. So, is there any method or module to do so?
import requests
r = requests.head(url)
print(r)

how to find if a s3 url is image or not in python 3

I have bunch of images in S3
https://s3.amazonaws.com/mybucket/image_datasets/im01.jpg
Although in this example it is an image, the url may or may not be an image.
From a python 3 client, I need to figure out if this url is an image or not. I was thinking if there is a way to read only the Content-type of that http request url similar to the one described here - in python 3.
Can anyone please suggest?
You would ordinarily make a HEAD request and check the Content-Type in the response. For images, the content type will start with image/.
import requests
def is_image(uri):
resp = requests.head(uri)
return resp.headers.get('content-type', '').startswith('image/')
You want to make a HEAD request, not a GET request, because you don't want to download the image. You just want some metadata. That’s what HEAD is for.
This is not specific to S3 but works with any HTTP URI which is publicly accessible.
I was thinking if there is a way to read only the Content-type of that http request url
Yes, you can use this kind of code (thanks to requests library) to read the Content-Type of the HTTP response:
import requests
r = requests.get('http://url/to/img.jpg')
print r.headers['Content-Type']
You will get a result like image/jpeg.
EDIT: as Dietrich Epp mentionned, better to use HEAD request instead of GET.

How to handle a post request from a Python program

So basically I would like to know how to handle a POST request from a python program, and store it on the website server so I can make a GET request to retrieve that information. I'm hoping you can help me. Currently this is my code:
import requests
url = 'mywebsitehere.com'
source_code = 'print('Hello World')
data = {'code': source_code, 'format': 'python'}
r = requests.post(url = url, data = data)
print(r.text)
I'm trying to send some code and the format for the code in the post request, but I'm not sure how to handle the post request once it reaches the website so other programs can access it with GET requests. I know how to actually send POST and GET requests in Python just not how to handle them once they reach the website/server. From my research, it seems like you have to make a PHP file or something and specify individual boxes or variables for the program to enter the information into.
I know it's a really noob question but I am just starting to get into more advanced stuff with Python and modules and stuff.
I'm going to learn more about general web development so instead of just barely understanding it I can get a good grasp on the post requests and actually develop my website into something custom rather than copying and pasting other peoples work without completely understanding it.
...also I'm not sure how to close a post to "answered" or something but yeah.

No JSON object could be decoded for the web APIs

I am new to python, and I am currently repeating all the solved examples from the book "python for data analysis"
in one example, I need to access the APIs from twitter search. Here is the code:
import requests
url='https://twitter.com/search?q=python%20pandas&src=typd'
resp=requests.get(url)
everything works okay up to here. problems comes when I use the json
import json
data=json.loads(resp.text)
then I received the error message ValueError: No JSON object could be decoded
I tried to switch to another url instead of the twitter, but I still receive the same error message.
does anyone have some ideas? thanks
You need a response with JSON content. To search Twitter this requires use of api.twitter.com, but you need to get an OAuth key. It's more complicated than the 5 lines of code you have. See http://nbviewer.ipython.org/github/chdoig/Mining-the-Social-Web-2nd-Edition/blob/master/ipynb/Chapter%201%20-%20Mining%20Twitter.ipynb

Categories